XSD Indicators
Indicators control the way how elements are to be organized in an XML document.
Indicators
There are seven types of indicators that fall into three broad categories.
Order Indicators
- All: Child elements can occur in any order.
- Choice: Only one of the child element can occur.
- Sequence: Child element can occur only in specified order.
Occurrence Indicators
- maxOccurs: Child element can occur only maxOccurs number of times.
- minOccurs: Child element must occur minOccurs number of times.
Group Indicators
- Group: Defines related set of elements.
- attributeGroup: Defines related set of attributes.
Order Indicators
Order indicators are used to define the order of the elements.
All Indicator
The <xs:all>
indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
When using the <xs:all>
indicator you can set the minOccurs
indicator to 0 or 1 and the maxOccurs
indicator can only be set to 1 (the minOccurs
and maxOccurs
are described later).
Choice Indicator
The <xs:choice>
indicator specifies that either one child element or another can occur:
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
Sequence Indicator
The <xs:sequence>
indicator specifies that the child elements must appear in a specific order:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Occurrence Indicators
Occurrence indicators are used to define how often an element can occur.
For all "Order" and "Group" indicators (i.e. any
, all
, choice
, sequence
, group name
, and group reference
) the default value for maxOccurs
and minOccurs
is 1.
maxOccurs Indicator
The maxOccurs
indicator specifies the maximum number of times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
In the example above, the child_name
element can occur a minimum of one time (the default value for minOccurs
is 1) and a maximum of ten times in the person
element
minOccurs Indicator
The minOccurs
indicator specifies the minimum number of times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
In the example above, the child_name
element can occur a minimum of zero times and a maximum of ten times in the person
element.
To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded"
statement.
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
Element groups are defined with the group
declaration in this way:
<xs:group name="groupname">
...
</xs:group>
For example:
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Attribute Groups
Attribute groups are defined with the attributeGroup
declaration in this way:
<xs:attributeGroup name="groupname">
...
</xs:attributeGroup>
For example:
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>