3

What is the difference between:

  <choice maxOccurs="unbounded">
     <element ref="test:A" maxOccurs="1"/>
  </choice>

And:

  <choice maxOccurs="1">
    <element ref="test:A" maxOccurs="unbounded"/>
  </choice>

For any practical purpose?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
jrm
  • 41
  • 4
  • Actually, the question is valid for any maxOccurs value between 1 and unbounded, – jrm Feb 15 '15 at 06:47

2 Answers2

3

Nothing, in that particular case, but the difference shows up when you add alternatives to the choice:

<choice maxOccurs="unbounded">
  <element ref="test:A" maxOccurs="1"/>
  <element ref="test:B" maxOccurs="1"/>
</choice>

would allow any number of A and B elements in any order, whereas

<choice maxOccurs="1">
  <element ref="test:A" maxOccurs="unbounded"/>
  <element ref="test:B" maxOccurs="unbounded"/>
</choice>

allows any number of As or any number of Bs but not a mixture of the two.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Thanks, that confirms my understanding. But then how come I get an error from this restriction: – jrm Feb 15 '15 at 19:45
  • (sorry for the formatting). I get an org.xml.sax.SAXParseException for this "Error for type 'cxT2'. The particle of the type is not a valid restriction of the particle of the base." – jrm Feb 15 '15 at 20:02
  • The language accepted by the restricted type is included in the larger language accepted by the original type (A{1,2} from cxT2 is accepted in [A]+ from cxT1) – jrm Feb 15 '15 at 20:15
3

There is no difference in that particular combination. Choosing a single alternative an unbounded number of times is the same as choosing once to allow an unbounded number of a single alternative.

How to Think of xsd:choice Cardinality

When @minOccurs or @maxOccurs appear on xs:choice, the minimum or maximum number of times the number of choices among the alternatives is constrained.

Then, for each such choice, the cardinality of the chosen child alternative comes into play.

Example Combinations

The following are some examples expressed in regular expression notation. Examples of valid sequences for the given combination are also provided.

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Regex: [AB]

Valid sequences include:

  • A
  • B

<choice minOccurs="0" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Regex: [AB]?

Valid sequences include:

  • nothing
  • A
  • B

<choice minOccurs="1" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Regex: [AB]+

Valid sequences include:

  • any combination containing at least one A or one B

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="unbounded"/>
  <element name="B" minOccurs="1" maxOccurs="unbounded"/>
</choice>

Regex: A+|B+

Valid sequences include:

  • A
  • AA
  • AAA
  • etc
  • B
  • BB
  • BBB
  • etc

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="0" maxOccurs="1"/>
  <element name="B" minOccurs="0" maxOccurs="unbounded"/>
</choice>

Regex: A?|B*

Valid sequences include:

  • nothing
  • A
  • B
  • BB
  • BBB
  • etc

<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Or

<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="0" maxOccurs="unbounded"/>
  <element name="B" minOccurs="0" maxOccurs="unbounded"/>
</choice>

Or

<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="unbounded"/>
</choice>

Etc

Regex: [AB]*

Valid sequences include:

  • nothing
  • any combination containing at least one A or one B

Default Values

The default value for both @minOccurs and @maxOccurs is 1.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    Excellent. One thing I'd like to add to this: repeatable choices and sequences often cause problems with tools like JAXB. So I generally advise not to use them in schemas. – lexicore Feb 15 '15 at 16:51
  • Agreed. Corrections made. Thanks, @IanRoberts! – kjhughes Feb 15 '15 at 17:57