XSD Indicators
Controlling element order and occurrence
🎯 What are XSD Indicators?
Indicators control how elements appear in XML documents. They define order, choice, grouping, and frequency of elements, giving you precise control over document structure and validation rules.
<!-- Indicators control structure -->
<xs:sequence>
<xs:element name="first" />
<xs:element name="second" />
</xs:sequence>
Order Indicators
Order indicators define the sequence and arrangement of child elements within a parent element. They control whether elements must appear in order or can be chosen.
🔸 Sequence Indicator (xs:sequence)
Elements must appear in the exact order specified:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="age" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<person>
<firstname>John</firstname>
<lastname>Doe</lastname>
<age>30</age>
</person>
🔸 Choice Indicator (xs:choice)
Only one of the specified elements can appear:
<xs:element name="contact">
<xs:complexType>
<xs:choice>
<xs:element name="email" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<xs:element name="address" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
Valid XML (choose one):
<contact>
<email>[email protected]</email>
</contact>
<!-- OR -->
<contact>
<phone>555-1234</phone>
</contact>
🔸 All Indicator (xs:all)
Elements can appear in any order, but each only once:
<xs:element name="product">
<xs:complexType>
<xs:all>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:decimal" />
<xs:element name="stock" type="xs:integer" />
</xs:all>
</xs:complexType>
</xs:element>
Valid XML (any order):
<product>
<price>29.99</price>
<name>Widget</name>
<stock>100</stock>
</product>
🔹 Occurrence Indicators
Occurrence indicators control how many times an element can appear using minOccurs and maxOccurs attributes:
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<!-- Required element (default) -->
<xs:element name="name" type="xs:string" />
<!-- Optional element (0 or 1) -->
<xs:element name="description" type="xs:string" minOccurs="0" />
<!-- Multiple elements (1 or more) -->
<xs:element name="book" type="xs:string" maxOccurs="unbounded" />
<!-- Optional multiple (0 or more) -->
<xs:element name="magazine" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<!-- Specific count (exactly 3) -->
<xs:element name="section" type="xs:string" minOccurs="3" maxOccurs="3" />
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<library>
<name>City Library</name>
<book>Book 1</book>
<book>Book 2</book>
<book>Book 3</book>
<section>Fiction</section>
<section>Non-Fiction</section>
<section>Reference</section>
</library>
🔹 Group Indicators
Group indicators combine multiple indicators for complex structures:
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="orderId" type="xs:string" />
<!-- Choice within sequence -->
<xs:choice>
<xs:element name="standardShipping" type="xs:string" />
<xs:element name="expressShipping" type="xs:string" />
</xs:choice>
<!-- Multiple items -->
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="productId" type="xs:string" />
<xs:element name="quantity" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML:
<order>
<orderId>ORD-001</orderId>
<expressShipping>Next Day</expressShipping>
<item>
<productId>P123</productId>
<quantity>2</quantity>
</item>
<item>
<productId>P456</productId>
<quantity>1</quantity>
</item>
</order>
🔹 Indicator Summary
xs:sequence
Elements in exact order
xs:choice
Pick one element only
xs:all
Any order, once each
minOccurs/maxOccurs
Control repetition