Meaning of minOccurs and maxOccurs for xsd:choice?
Asked Answered
S

2

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?

Sucker answered 15/2, 2015 at 6:26 Comment(1)
Actually, the question is valid for any maxOccurs value between 1 and unbounded,Sucker
D
5

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.

Diffractometer answered 15/2, 2015 at 16:41 Comment(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.Bunn
E
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.

Enrico answered 15/2, 2015 at 10:58 Comment(4)
Thanks, that confirms my understanding. But then how come I get an error from this restriction:Sucker
<complexType name="cxT1"> <choice maxOccurs="unbounded"> <element ref="test:A" maxOccurs="1"/> </choice> </complexType> <complexType name="cxT2"> <complexContent> <restriction base="test:cxT1"> <choice maxOccurs="1"> <element ref="test:A" maxOccurs="2"/> </choice> </restriction> </complexContent> </complexType>Sucker
(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."Sucker
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)Sucker

© 2022 - 2024 — McMap. All rights reserved.