more work on the automata interfaces and debug of counted choices added a

* xmlregexp.c xmlschemas.c include/libxml/xmlautomata.h: more work
  on the automata interfaces and debug of counted choices
* test/schemas/* result/schemas/*: added a number of tests
Daniel
diff --git a/ChangeLog b/ChangeLog
index 8953de0..3f8e49c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Apr 17 18:26:07 CEST 2002 Daniel Veillard <daniel@veillard.com>
+
+	* xmlregexp.c xmlschemas.c include/libxml/xmlautomata.h: more work
+	  on the automata interfaces and debug of counted choices
+	* test/schemas/* result/schemas/*: added a number of tests
+
 Wed Apr 17 11:03:03 CEST 2002 Daniel Veillard <daniel@veillard.com>
 
 	* xmlschemas.c xmlschemastypes.c include/libxml/xmlschemas.h:
diff --git a/include/libxml/xmlautomata.h b/include/libxml/xmlautomata.h
index ad3acf7..853acc4 100644
--- a/include/libxml/xmlautomata.h
+++ b/include/libxml/xmlautomata.h
@@ -63,7 +63,17 @@
 xmlAutomataStatePtr	xmlAutomataNewEpsilon	(xmlAutomataPtr am,
 						 xmlAutomataStatePtr from,
 						 xmlAutomataStatePtr to);
-int			xmlAutomataNewCounter	(xmlAutomataPtr am);
+xmlAutomataStatePtr	xmlAutomataNewCountedTrans(xmlAutomataPtr am,
+						 xmlAutomataStatePtr from,
+						 xmlAutomataStatePtr to,
+						 int counter);
+xmlAutomataStatePtr	xmlAutomataNewCounterTrans(xmlAutomataPtr am,
+						 xmlAutomataStatePtr from,
+						 xmlAutomataStatePtr to,
+						 int counter);
+int			xmlAutomataNewCounter	(xmlAutomataPtr am,
+						 int min,
+						 int max);
 
 xmlRegexpPtr		xmlAutomataCompile	(xmlAutomataPtr am);
 
diff --git a/result/schemas/choice_0_5 b/result/schemas/choice_0_5
new file mode 100644
index 0000000..acaaddd
--- /dev/null
+++ b/result/schemas/choice_0_5
@@ -0,0 +1 @@
+./test/schemas/choice_5.xml fails to validate
diff --git a/result/schemas/choice_0_5.err b/result/schemas/choice_0_5.err
new file mode 100644
index 0000000..a4025c9
--- /dev/null
+++ b/result/schemas/choice_0_5.err
@@ -0,0 +1,24 @@
+Type of choice 3 : ./test/schemas/choice_0.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_0.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_0.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_0.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_0_6 b/result/schemas/choice_0_6
new file mode 100644
index 0000000..ee4ceae
--- /dev/null
+++ b/result/schemas/choice_0_6
@@ -0,0 +1 @@
+./test/schemas/choice_6.xml fails to validate
diff --git a/result/schemas/choice_0_6.err b/result/schemas/choice_0_6.err
new file mode 100644
index 0000000..a4025c9
--- /dev/null
+++ b/result/schemas/choice_0_6.err
@@ -0,0 +1,24 @@
+Type of choice 3 : ./test/schemas/choice_0.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_0.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_0.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_0.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_1_0 b/result/schemas/choice_1_0
new file mode 100644
index 0000000..eda062e
--- /dev/null
+++ b/result/schemas/choice_1_0
@@ -0,0 +1 @@
+./test/schemas/choice_0.xml validates
diff --git a/result/schemas/choice_1_0.err b/result/schemas/choice_1_0.err
new file mode 100644
index 0000000..306b246
--- /dev/null
+++ b/result/schemas/choice_1_0.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check succeeded
diff --git a/result/schemas/choice_1_1 b/result/schemas/choice_1_1
new file mode 100644
index 0000000..e4338de
--- /dev/null
+++ b/result/schemas/choice_1_1
@@ -0,0 +1 @@
+./test/schemas/choice_1.xml validates
diff --git a/result/schemas/choice_1_1.err b/result/schemas/choice_1_1.err
new file mode 100644
index 0000000..d9e0051
--- /dev/null
+++ b/result/schemas/choice_1_1.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: b, b, b
+Element doc content check succeeded
diff --git a/result/schemas/choice_1_2 b/result/schemas/choice_1_2
new file mode 100644
index 0000000..4071315
--- /dev/null
+++ b/result/schemas/choice_1_2
@@ -0,0 +1 @@
+./test/schemas/choice_2.xml fails to validate
diff --git a/result/schemas/choice_1_2.err b/result/schemas/choice_1_2.err
new file mode 100644
index 0000000..3d3bd98
--- /dev/null
+++ b/result/schemas/choice_1_2.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_1_3 b/result/schemas/choice_1_3
new file mode 100644
index 0000000..bef604b
--- /dev/null
+++ b/result/schemas/choice_1_3
@@ -0,0 +1 @@
+./test/schemas/choice_3.xml fails to validate
diff --git a/result/schemas/choice_1_3.err b/result/schemas/choice_1_3.err
new file mode 100644
index 0000000..3d3bd98
--- /dev/null
+++ b/result/schemas/choice_1_3.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_1_4 b/result/schemas/choice_1_4
new file mode 100644
index 0000000..36b3827
--- /dev/null
+++ b/result/schemas/choice_1_4
@@ -0,0 +1 @@
+./test/schemas/choice_4.xml validates
diff --git a/result/schemas/choice_1_4.err b/result/schemas/choice_1_4.err
new file mode 100644
index 0000000..610e947
--- /dev/null
+++ b/result/schemas/choice_1_4.err
@@ -0,0 +1,24 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+Element doc content check succeeded
diff --git a/result/schemas/choice_1_5 b/result/schemas/choice_1_5
new file mode 100644
index 0000000..acaaddd
--- /dev/null
+++ b/result/schemas/choice_1_5
@@ -0,0 +1 @@
+./test/schemas/choice_5.xml fails to validate
diff --git a/result/schemas/choice_1_5.err b/result/schemas/choice_1_5.err
new file mode 100644
index 0000000..3d3bd98
--- /dev/null
+++ b/result/schemas/choice_1_5.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_1_6 b/result/schemas/choice_1_6
new file mode 100644
index 0000000..ee4ceae
--- /dev/null
+++ b/result/schemas/choice_1_6
@@ -0,0 +1 @@
+./test/schemas/choice_6.xml fails to validate
diff --git a/result/schemas/choice_1_6.err b/result/schemas/choice_1_6.err
new file mode 100644
index 0000000..3d3bd98
--- /dev/null
+++ b/result/schemas/choice_1_6.err
@@ -0,0 +1,25 @@
+Type of choice 3 : ./test/schemas/choice_1.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_1.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_1.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+4 states:
+ state: FINAL 0, 3 transitions:
+  trans: atom 0, to 2
+  trans: atom 1, to 3
+  trans: removed
+ state: FINAL 1, 0 transitions:
+ state: FINAL 2, 1 transitions:
+  trans: removed
+ state: FINAL 3, 1 transitions:
+  trans: removed
+0 counters:
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/result/schemas/choice_2_0 b/result/schemas/choice_2_0
new file mode 100644
index 0000000..eda062e
--- /dev/null
+++ b/result/schemas/choice_2_0
@@ -0,0 +1 @@
+./test/schemas/choice_0.xml validates
diff --git a/result/schemas/choice_2_0.err b/result/schemas/choice_2_0.err
new file mode 100644
index 0000000..9596021
--- /dev/null
+++ b/result/schemas/choice_2_0.err
@@ -0,0 +1,40 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check succeeded
diff --git a/result/schemas/choice_2_1 b/result/schemas/choice_2_1
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/schemas/choice_2_1
diff --git a/result/schemas/choice_2_1.err b/result/schemas/choice_2_1.err
new file mode 100644
index 0000000..393f6ff
--- /dev/null
+++ b/result/schemas/choice_2_1.err
@@ -0,0 +1,42 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: b, b, b
+Element doc content check succeeded
+(nil) : Freed()
+xmlMallocBreakpoint reached on block 0
diff --git a/result/schemas/choice_2_2 b/result/schemas/choice_2_2
new file mode 100644
index 0000000..c58e154
--- /dev/null
+++ b/result/schemas/choice_2_2
@@ -0,0 +1 @@
+./test/schemas/choice_2.xml validates
diff --git a/result/schemas/choice_2_2.err b/result/schemas/choice_2_2.err
new file mode 100644
index 0000000..041d017
--- /dev/null
+++ b/result/schemas/choice_2_2.err
@@ -0,0 +1,41 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: b, b, b
+Element doc content check succeeded
diff --git a/result/schemas/choice_2_3 b/result/schemas/choice_2_3
new file mode 100644
index 0000000..1058f7e
--- /dev/null
+++ b/result/schemas/choice_2_3
@@ -0,0 +1 @@
+./test/schemas/choice_3.xml validates
diff --git a/result/schemas/choice_2_3.err b/result/schemas/choice_2_3.err
new file mode 100644
index 0000000..1d2e304
--- /dev/null
+++ b/result/schemas/choice_2_3.err
@@ -0,0 +1,41 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+Element doc content check succeeded
diff --git a/result/schemas/choice_2_4 b/result/schemas/choice_2_4
new file mode 100644
index 0000000..0643e3b
--- /dev/null
+++ b/result/schemas/choice_2_4
@@ -0,0 +1 @@
+./test/schemas/choice_4.xml fails to validate
diff --git a/result/schemas/choice_2_4.err b/result/schemas/choice_2_4.err
new file mode 100644
index 0000000..d4ef043
--- /dev/null
+++ b/result/schemas/choice_2_4.err
@@ -0,0 +1,39 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+Element doc content check failed
diff --git a/result/schemas/choice_2_5 b/result/schemas/choice_2_5
new file mode 100644
index 0000000..67e79ba
--- /dev/null
+++ b/result/schemas/choice_2_5
@@ -0,0 +1 @@
+./test/schemas/choice_5.xml validates
diff --git a/result/schemas/choice_2_5.err b/result/schemas/choice_2_5.err
new file mode 100644
index 0000000..e2fc216
--- /dev/null
+++ b/result/schemas/choice_2_5.err
@@ -0,0 +1,42 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: b, b, b
+xmlSchemaValidateCallback: a, a, a
+Element doc content check succeeded
diff --git a/result/schemas/choice_2_6 b/result/schemas/choice_2_6
new file mode 100644
index 0000000..ee4ceae
--- /dev/null
+++ b/result/schemas/choice_2_6
@@ -0,0 +1 @@
+./test/schemas/choice_6.xml fails to validate
diff --git a/result/schemas/choice_2_6.err b/result/schemas/choice_2_6.err
new file mode 100644
index 0000000..2d03b13
--- /dev/null
+++ b/result/schemas/choice_2_6.err
@@ -0,0 +1,54 @@
+Type of choice 3 : ./test/schemas/choice_2.xsd:6 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Type of anontype1 : ./test/schemas/choice_2.xsd:4 :elements
+Type of sequence 2 : ./test/schemas/choice_2.xsd:5 :elements
+Building content model for doc
+Content model of doc:
+ regexp: '(null)' 
+2 atoms:
+ 00  atom: string once 'a' 
+ 01  atom: string once 'b' 
+5 states:
+ state: 0, 2 transitions:
+  trans: atom 0, to 3
+  trans: atom 1, to 4
+ state: FINAL 1, 0 transitions:
+ state: 2, 4 transitions:
+  trans: removed
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 3, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+ state: 4, 6 transitions:
+  trans: removed
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+  trans: count based 0, epsilon to 1
+  trans: counted 0, atom 0, to 3
+  trans: counted 0, atom 1, to 4
+1 counters:
+ 0: min 0 max 2
+Building content model for a
+Building content model for b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: b, b, b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: b, b, b
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+xmlSchemaValidateCallback: a, a, a
+Element doc content check failed
diff --git a/test/schemas/choice_0.xsd b/test/schemas/choice_0.xsd
index 7f690f4..1491b6b 100644
--- a/test/schemas/choice_0.xsd
+++ b/test/schemas/choice_0.xsd
@@ -3,7 +3,7 @@
   <xs:element name="doc">
     <xs:complexType>
       <xs:sequence>
-        <xs:choice minOccurs="1" maxOccurs="unbounded">
+        <xs:choice minOccurs="1" maxOccurs="1">
           <xs:element name="a"/>
           <xs:element name="b"/>
         </xs:choice>
diff --git a/test/schemas/choice_1.xsd b/test/schemas/choice_1.xsd
new file mode 100644
index 0000000..00614d0
--- /dev/null
+++ b/test/schemas/choice_1.xsd
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+  <xs:element name="doc">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:choice minOccurs="0" maxOccurs="1">
+          <xs:element name="a"/>
+          <xs:element name="b"/>
+        </xs:choice>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>
diff --git a/test/schemas/choice_2.xsd b/test/schemas/choice_2.xsd
new file mode 100644
index 0000000..9f2b094
--- /dev/null
+++ b/test/schemas/choice_2.xsd
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+  <xs:element name="doc">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:choice minOccurs="1" maxOccurs="3">
+          <xs:element name="a"/>
+          <xs:element name="b"/>
+        </xs:choice>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>
diff --git a/test/schemas/choice_5.xml b/test/schemas/choice_5.xml
new file mode 100644
index 0000000..3345a68
--- /dev/null
+++ b/test/schemas/choice_5.xml
@@ -0,0 +1,6 @@
+<doc>
+<a/>
+<b/>
+<a/>
+</doc>
+
diff --git a/test/schemas/choice_6.xml b/test/schemas/choice_6.xml
new file mode 100644
index 0000000..394153d
--- /dev/null
+++ b/test/schemas/choice_6.xml
@@ -0,0 +1,7 @@
+<doc>
+<a/>
+<b/>
+<a/>
+<a/>
+</doc>
+
diff --git a/xmlregexp.c b/xmlregexp.c
index 1139e15..31657ea 100644
--- a/xmlregexp.c
+++ b/xmlregexp.c
@@ -1144,20 +1144,41 @@
 	     * Don't remove counted transitions
 	     * Don't loop either
 	     */
-	    if ((to->trans[transnr].count < 0) &&
-	        (to->trans[transnr].to != fromnr)) {
+	    if (to->trans[transnr].to != fromnr) {
+		if (to->trans[transnr].count >= 0) {
+		    int newto = to->trans[transnr].to;
+
+		    xmlRegStateAddTrans(ctxt, from, NULL,
+					ctxt->states[newto], 
+					-1, to->trans[transnr].count);
+		} else {
 #ifdef DEBUG_REGEXP_GRAPH
-		printf("Found epsilon trans %d from %d to %d\n",
-		       transnr, tonr, to->trans[transnr].to);
+		    printf("Found epsilon trans %d from %d to %d\n",
+			   transnr, tonr, to->trans[transnr].to);
 #endif
-		xmlFAReduceEpsilonTransitions(ctxt, fromnr,
-				      to->trans[transnr].to, counter);
+		    if (to->trans[transnr].counter >= 0) {
+			xmlFAReduceEpsilonTransitions(ctxt, fromnr,
+					      to->trans[transnr].to,
+					      to->trans[transnr].counter);
+		    } else {
+			xmlFAReduceEpsilonTransitions(ctxt, fromnr,
+					      to->trans[transnr].to,
+					      counter);
+		    }
+		}
 	    }
 	} else {
 	    int newto = to->trans[transnr].to;
 
-	    xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, 
-		                ctxt->states[newto], counter, -1);
+	    if (to->trans[transnr].counter >= 0) {
+		xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, 
+				    ctxt->states[newto], 
+				    to->trans[transnr].counter, -1);
+	    } else {
+		xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, 
+				    ctxt->states[newto], counter, -1);
+	    }
+
 	}
     }
     to->mark = XML_REGEXP_MARK_NORMAL;
@@ -1966,7 +1987,7 @@
 	 * still have epsilon like transition for counted transitions
 	 * on counters, in that case don't break too early.
 	 */
-	if (value == NULL)
+	if ((value == NULL) && (exec->counts == NULL))
 	    goto rollback;
 
 	exec->transcount = 0;
@@ -2612,7 +2633,7 @@
     } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
 	(cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
 	(cur == 'w') || (cur == 'W')) {
-	xmlRegAtomType type;
+	xmlRegAtomType type = XML_REGEXP_ANYSPACE;
 
 	switch (cur) {
 	    case 's': 
@@ -3444,9 +3465,78 @@
     return(to);
 }
 
-#if 0
-int			xmlAutomataNewCounter	(xmlAutomataPtr am);
-#endif
+/**
+ * xmlAutomataNewCounter:
+ * @am: an automata
+ * @min:  the minimal value on the counter
+ * @max:  the maximal value on the counter
+ *
+ * Create a new counter
+ *
+ * Returns the counter number or -1 in case of error
+ */
+int		
+xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
+    int ret;
+
+    if (am == NULL)
+	return(-1);
+
+    ret = xmlRegGetCounter(am);
+    if (ret < 0)
+	return(-1);
+    am->counters[ret].min = min;
+    am->counters[ret].max = max;
+    return(ret);
+}
+
+/**
+ * xmlAutomataNewCountedTrans:
+ * @am: an automata
+ * @from: the starting point of the transition
+ * @to: the target point of the transition or NULL
+ * @counter: the counter associated to that transition
+ *
+ * If @to is NULL, this create first a new target state in the automata
+ * and then adds an epsilon transition from the @from state to the target state
+ * which will increment the counter provided
+ *
+ * Returns the target state or NULL in case of error
+ */
+xmlAutomataStatePtr
+xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
+		xmlAutomataStatePtr to, int counter) {
+    if ((am == NULL) || (from == NULL) || (counter < 0))
+	return(NULL);
+    xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
+    if (to == NULL)
+	return(am->state);
+    return(to);
+}
+
+/**
+ * xmlAutomataNewCounterTrans:
+ * @am: an automata
+ * @from: the starting point of the transition
+ * @to: the target point of the transition or NULL
+ * @counter: the counter associated to that transition
+ *
+ * If @to is NULL, this create first a new target state in the automata
+ * and then adds an epsilon transition from the @from state to the target state
+ * which will be allowed only if the counter is within the right range.
+ *
+ * Returns the target state or NULL in case of error
+ */
+xmlAutomataStatePtr
+xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
+		xmlAutomataStatePtr to, int counter) {
+    if ((am == NULL) || (from == NULL) || (counter < 0))
+	return(NULL);
+    xmlFAGenerateCountedTransition(am, from, to, counter);
+    if (to == NULL)
+	return(am->state);
+    return(to);
+}
 
 /**
  * xmlAutomataCompile:
diff --git a/xmlschemas.c b/xmlschemas.c
index 4ccc1f1..e47b382 100644
--- a/xmlschemas.c
+++ b/xmlschemas.c
@@ -2995,12 +2995,43 @@
 	     * iterate over the subtypes and remerge the end with an
 	     * epsilon transition
 	     */
-	    subtypes = type->subtypes;
-	    while (subtypes != NULL) {
-		ctxt->state = start;
-		xmlSchemaBuildAContentModel(subtypes, ctxt, name);
-		xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end);
-		subtypes = subtypes->next;
+	    if (type->maxOccurs == 1) {
+		subtypes = type->subtypes;
+		while (subtypes != NULL) {
+		    ctxt->state = start;
+		    xmlSchemaBuildAContentModel(subtypes, ctxt, name);
+		    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, end);
+		    subtypes = subtypes->next;
+		}
+	    } else {
+		int counter;
+		xmlAutomataStatePtr hop;
+
+		/*
+		 * use a counter to keep track of the number of transtions
+		 * which went through the choice.
+		 */
+		if (type->minOccurs < 1) {
+		    counter = xmlAutomataNewCounter(ctxt->am, 0,
+			                            type->maxOccurs - 1);
+		} else {
+		    counter = xmlAutomataNewCounter(ctxt->am,
+			    type->minOccurs - 1, type->maxOccurs - 1);
+		}
+		hop = xmlAutomataNewState(ctxt->am);
+
+		subtypes = type->subtypes;
+		while (subtypes != NULL) {
+		    ctxt->state = start;
+		    xmlSchemaBuildAContentModel(subtypes, ctxt, name);
+		    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, hop);
+		    subtypes = subtypes->next;
+		}
+		xmlAutomataNewCountedTrans(ctxt->am, hop, start, counter);
+		xmlAutomataNewCounterTrans(ctxt->am, hop, end, counter);
+	    }
+	    if (type->minOccurs == 0) {
+		xmlAutomataNewEpsilon(ctxt->am, start, end);
 	    }
 	    ctxt->state = end;
 	    break;