If I have an element with one or more subelements, should the min/maxoccurs attributes be on the xsd:sequence element, the xsd:element, both, or neither?
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded"> <!-- here? -->
<xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/> <!-- or here? -->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Definition and Usage The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.
The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once.
<xsd:all> ElementAllows the elements in the group to appear (or not appear) in any order in the containing element. Copy. <all id = ID maxOccurs= 1: 1 minOccurs= (0 | 1): 1 {any attributes with non-schema Namespace...
In almost all circumstances, you want to put the min/max Occurs on the element within a sequence and not on the sequence. Using your example:
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
This is unequivocal. If you have a series of book elements in a row, you can point to exactly which schema item is producing them. However:
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded"/>
<xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Here, if you have two "book
" elements in a row, do you have two sequence
s in a row, or one sequence
with two book
elements? This fails the Unique Particle Attribution requirement.
Finally, if you put the min/max Occurs on the sequence and you later add an additional element:
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded"/>
<xsd:element ref="book"/>
<xsd:element ref="ebook"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
then this may allow the following XML, which is probably not what you intend:
<books>
<book/><ebook/><book/><ebook/><book/><ebook/><book/><ebook/>
</books>
whereas if you have:
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence/>
<xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element ref="ebook" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
then it is clear and unambiguous what you intend: A sequence of one or more book
elements followed by a sequence of one or more ebook
elements.
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