Occurrence Constraints
One feature of schemas that should be welcome to DTD
developers is the ability to explicitly set the minimum and maximum
number of times an element may occur at a particular point in a
document using minOccurs and
maxOccurs attributes of the
xs:element element. For example,
this declaration adds an optional middle name to the fullName element:
<xs:element name="fullName">
<xs:complexType>
<xs:sequence>
<xs:element name="first" type="addr:nameComponent"/>
<xs:element name="middle" type="addr:nameComponent"
minOccurs="0"/>
<xs:element name="last" type="addr:nameComponent"/>
</xs:sequence>
</xs:complexType>
</xs:element>Notice that the element declaration for the middle element has a minOccurs value of 0. The default value
for both minOccurs and maxOccurs is 1, if they are not provided
explicitly. Therefore, setting minOccurs to 0 means that the middle element may appear 0 to 1 times.
This is equivalent to using the ?
operator in a DTD declaration. Another possible value for the
maxOccurs attribute is unbounded, which indicates that the
element in question may appear an unlimited number of times. This
value is used to produce the same effect as the * and +
operators in a DTD declaration. The advantage over DTDs comes when
you use values other than 0, 1, or unbounded, letting you specify things like
“this element must appear at least twice but no more than four
times.”