XML Schema minOccurs / maxOccurs default values
XmlXsdXml Problem Overview
I'm wondering how the XML Schema specification handles these cases:
<xsd:element minOccurs="1" name="asdf"/>
No maxOccurs given -> Is this the cardinality [1..1]?
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
I suppose this is simply invalid?
<xsd:element maxOccurs="2" name="asdf"/>
Is this the cardinality [0..2] or [1..2]?
Is there an "official" definition on how the XML Schema spec handles these cases?
Xml Solutions
Solution 1 - Xml
The default values for minOccurs
and maxOccurs
are 1. Thus:
<xsd:element minOccurs="1" name="asdf"/>
cardinality is [1-1] Note: if you specify only minOccurs attribute, it can't be greater than 1, because the default value for maxOccurs is 1.
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
invalid
<xsd:element maxOccurs="2" name="asdf"/>
cardinality is [1-2] Note: if you specify only maxOccurs attribute, it can't be smaller than 1, because the default value for minOccurs is 1.
<xsd:element minOccurs="0" maxOccurs="0"/>
is a valid combination which makes the element prohibited.
For more info see http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
Solution 2 - Xml
New, expanded answer to an old, commonly asked question...
Default Values
- Occurrence constraints
minOccurs
andmaxOccurs
default to1
.
Common Cases Explained
<xsd:element name="A"/>
means A
is required and must appear exactly once.
<xsd:element name="A" minOccurs="0"/>
means A
is optional and may appear at most once.
<xsd:element name="A" maxOccurs="unbounded"/>
means A
is required and may repeat an unlimited number of times.
<xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>
means A
is optional and may repeat an unlimited number of times.
See Also
-
> In general, an element is required to appear when the value of > minOccurs is 1 or more. The maximum number of times an element may > appear is determined by the value of a maxOccurs attribute in its > declaration. This value may be a positive integer such as 41, or the > term unbounded to indicate there is no maximum number of occurrences. > 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. Be sure > that if you specify a value for only the minOccurs attribute, it is > less than or equal to the default value of maxOccurs, i.e. it is 0 or 1. > Similarly, if you specify a value for only the maxOccurs attribute, it must be greater than or equal to the default value of minOccurs, > i.e. 1 or more. If both attributes are omitted, the element must > appear exactly once.
-
W3C XML Schema Part 1: Structures Second Edition
<element maxOccurs = (nonNegativeInteger | unbounded) : 1 minOccurs = nonNegativeInteger : 1 > </element>
Solution 3 - Xml
Short answer:
As written in xsd:
<xs:attribute name="minOccurs" type="xs:nonNegativeInteger" use="optional" default="1"/>
<xs:attribute name="maxOccurs" type="xs:allNNI" use="optional" default="1"/>
If you provide an attribute with number, then the number is boundary. Otherwise attribute should appear exactly once.