XSD Indicators
Through indicators, we can control the way elements are used in a document.
Indicators
There are seven types of indicators:
Order Indicators:
- All
- Choice
- Sequence
Occurrence Indicators:
- maxOccurs
- minOccurs
Group Indicators:
- Group name
- attributeGroup name
Order Indicators
Order indicators are used to define the order of elements.
All Indicator
The <all> indicator specifies that child elements can appear in any order, and each child element must appear only once:
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
Note:When using the <all> indicator, you can set <minOccurs> to 0 or 1, and you can only set the <maxOccurs> indicator to 1 (<minOccurs> and <maxOccurs> will be explained later).
Choice Indicator
The <choice> indicator specifies that either one child element or another child element can appear (either/or):
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
Sequence Indicator
<sequence> specifies that child elements must appear in a specific order:
<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 appear.
Note:For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference), the default values of maxOccurs and minOccurs are both 1.
maxOccurs Indicator
The <maxOccurs> indicator can specify the maximum number of times an element can appear:
<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>
The above example indicates that the child element "child_name" can appear at least once in the "person" element (where the default value of minOccurs is 1) and at most 10 times.
minOccurs Indicator
The <minOccurs> indicator can specify the minimum number of times an element can appear:
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The above example indicates that the child element "child_name" can appear at least 0 times and at most 10 times in the "person" element.
Tip:If you want an element to have unlimited occurrences, use the declaration maxOccurs="unbounded":
A practical example:
An XML file named "Myfamily.xml":
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>
The XML file above contains a root element named "persons". Inside this root element, we define three "person" elements. Each "person" element must contain a "full_name" element, and it can contain up to 5 "child_name" elements.
This is the schema file "family.xsd":
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Group Indicators
Group indicators are used to define related sets of elements.
Element Groups
Element groups are defined using the group declaration:
...
</xs:group>
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", which defines a set of elements that must appear in an exact order:
<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>
After you have defined the group, you can reference it in another definition:
<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 using the attributeGroup declaration:
...
</xs:attributeGroup>
The following example defines an attribute group named "personattrgroup":
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
After you have defined the attribute group, you can reference it in another definition, like this:
<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>