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>
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:
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:
Here, if you have two ‘
book‘ elements in a row, do you have twosequences in a row, or onesequencewith twobookelements? 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:
then this may allow the following XML, which is probably not what you intend:
whereas if you have:
then it is clear and unambiguous what you intend: A sequence of one or more
bookelements followed by a sequence of one or moreebookelements.