XSD Indicators
Through indicators, we can control how elements are used in the document.
Indicators
There are seven 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 sequence of elements.
All Indicator
The <all>
indicator specifies that child elements can appear in any order, and each child element must appear only once:
Note: When using the <all>
indicator, you can set <minOccurs>
to 0 or 1, and <maxOccurs>
can only be set to 1 (later explained are <minOccurs>
and <maxOccurs>
).
Choice Indicator
The <choice>
indicator specifies that either one child element or another can appear (but not both):
Sequence Indicator
The <sequence>
indicator specifies that child elements must appear in a specific order:
Occurrence Indicators
Occurrence indicators are used to define the frequency of an element's appearance.
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference), the default values for maxOccurs and minOccurs are both 1.
maxOccurs Indicator
The <maxOccurs>
indicator can specify the maximum number of times an element can appear:
The example above indicates that the child element "child_name" can appear at least once (the default value for minOccurs is 1) and up to 10 times within the "person" element.
minOccurs Indicator
The <minOccurs>
indicator can specify the minimum number of times an element can appear:
The example above indicates that the child element "child_name" can appear at least 0 times and up to 10 times within the "person" element.
Tip: To make an element's occurrence unlimited, use the declaration maxOccurs="unbounded":
A Practical Example:
An XML file named "Myfamily.xml":
This XML file contains a root element named "persons". Inside this root element, we define three "person" elements. Each "person" element must contain one "full_name" element and can contain up to five "child_name" elements.
Here is the schema file "family.xsd":
Group Indicators
Group indicators are used to define related batches of elements.
Element Group
Element groups are defined through the group declaration:
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", which specifies a set of elements that must appear in an exact order:
After defining the group, you can reference it in another definition:
Attribute Group
Attribute groups are defined through the attributeGroup declaration:
The following example defines an attribute group named "personattrgroup":
After defining the attribute group, you can reference it in another definition, like this: