Default minOccurs and maxOccurs in XML Schema
This article explains the default values for the
minOccurs and maxOccurs attributes in an XML
Schema Definition (XSD). Understanding these defaults is essential for
defining element cardinality correctly, avoiding schema validation
errors, and designing predictable XML document structures.
In an XML Schema element declaration, the default value for both
minOccurs and maxOccurs is
1.
Understanding the Defaults
minOccurs="1": Defines the minimum number of times an element can appear. Because the default is1, the element is mandatory by default and must appear at least once.maxOccurs="1": Defines the maximum number of times an element can appear. Because the default is1, the element can appear at most once.
Behavior When Omitted
If you declare an element in an XSD without explicitly specifying either attribute:
<xs:element name="username" type="xs:string"/>The XML Schema processor automatically treats it as:
<xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1"/>This means the element is strictly required and must appear exactly once within its parent container.
Common Modifications
To change this default behavior, you must explicitly set the attributes:
- Optional Element: Set
minOccurs="0"and leavemaxOccurs="1"(or omit it). The element can appear 0 or 1 time. - Unbounded/List Element: Set
maxOccurs="unbounded". When paired withminOccurs="0", the element is entirely optional and can repeat indefinitely. When paired withminOccurs="1", the element must appear at least once and can repeat indefinitely.