In this example below,
<xs:complexType>
<xs:choice minOccurs="3" maxOccurs="unbounded">
<xs:element ref="Start"/>
<xs:element ref="Center"/>
<xs:element ref="End"/>
<xs:element ref="PI" minOccurs="0"/>
<xs:element ref="Feature" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
What happens when choice has minOccurs > 1? Does this mean element "Start" can occur 3 times?
An array with a varying number of elements is represented in the XML schema by using the minOccurs and maxOccurs attributes on the element declaration: The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer.
Using <xsd:choice> in an XSD It has to be defined in XML schema definition of a nested XML content. The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.
Definition and Usage XML Schema choice element allows only one of the elements contained in the <choice> declaration to be present within the containing element.
<xsd:choice> ElementAllows one and only one of the elements contained in the selected group to be present within the containing element. Copy. <choice id = ID maxOccurs= (nonNegativeInteger | unbounded) : 1 minOccurs= nonNegativeInteger : 1 {any attributes with non-schema Namespace}...> Content: (annotation?, (
What happens when choice has minOccurs > 1? Does this mean element "Start" can occur 3 times?
Yes <Start>
can occur 3 or more times! Having minOccurs of choice as more than 1 allows set of elements to appear more than once or different elements appear multiple times.
Detailed explanation:
In the above example you have applied minOccurs as 3 for <Choice>
and maxOccurs as unbounded! That means .. Child elements listed under <Choice>
, any three of them or any tag repeated three times should appear under their parent! sample XML are listed below:
Let us assume that these set of tags appear under a node called <parent>
then:
<parent>
<Start>Start1</Start>
<Center>Center1</Center>
<End>End1</End>
</parent>
-------- OR --------
<parent>
<Center>Start1</Center>
<Center>Center1</Center>
<Feature>End1</Feature>
</parent>
-------- OR --------
<parent>
<Start>Start1</Start>
<Start>Start1</Start>
<Start>Start1</Start>
<Start>Start1</Start>
</parent>
all the above combinations are valid!
BUT you have also defined minOccurs="0" for 'PI' and 'Feature' ..
This addition causes Validator to pass parent with no child elements as well. ie:
<parent>
</parent>
If you remove minOccurs from those two elements then validation forces you to include minimum of 3 tags to be included under parent.
Also having maxOccurs="unbounded"
for Feature
Element is of no use! The behavior won't change if you add it or take it off..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With