XSD Indicators

The way elements are to be used in documents with indicators can be controlled.

Indicators:

In XSD, there are seven indicators:

Order indicators:

  • All
  • Choice
  • Sequence

Occurrence indicators:

  • maxOccurs
  • minOccurs

Group indicators:

  • Group name
  • attributeGroup name

Order Indicators:

To define the order of the elements, the order indicators are used. The default value for maxOccurs and minOccurs for all “Order” and “Group” indicators (any, all, choice, sequence, group name, and group reference) is 1.

All Indicator:

To indicate that the child elements can appear in any order and that each child element must occur only once, the <all> indicator is used. The <minOccurs> indicator can be set to 0 or 1 and the <maxOccurs> indicator can only be set to 1 while using the <all> indicator.

Example:

<xs:element name="student">
  <xs:complextype>
    <xs:all>
      <xs:element name="firstname" type="xs:string"></xs:element>
      <xs:element name="lastname" type="xs:string"></xs:element>
    </xs:all>
  </xs:complextype>
</xs:element>

Choice Indicator:

To define that either one child element or another can occur, the <choice> indicator is used.

Example:

<xs:element name="student">
  <xs:complextype>
    <xs:choice>
      <xs:element name="gender" type="gender"></xs:element>
      <xs:element name="citizen" type="citizen"></xs:element>
    </xs:choice>
  </xs:complextype>
</xs:element>

Sequence Indicator:

To specify that the child elements must appear in a specific order, the <sequence> indicator is used.

Example:

<xs:element name="student">
   <xs:complextype>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"></xs:element>
      <xs:element name="lastname" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Occurrence Indicators:

To define how often an element can occur, the occurrence indicators are used.

maxOccurs Indicator:

To define the maximum number of times an element can occur, the <maxOccurs> indicator is used.

Example:

<xs:element name="student">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"></xs:element>
      <xs:element name="sibling_name" type="xs:string" maxOccurs="10"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, we are defining the “sibling_name” element to occur a minimum of one time, i.e., the default value for minOccurs is 1 and a maximum of ten times in the “student” element.

minOccurs Indicator:

To define the minimum number of times an element can occur, the <minOccurs> indicator is used.

Example:

<xs:element name="student">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"></xs:element>
      <xs:element name="sibling_name" type="xs:string"
     maxOccurs="10" minOccurs="0"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, we are defining the “sibling_name” element to occur a minimum of zero times and a maximum of ten times in the “student” element.

maxOccurs=”unbounded” statement:

We can use the maxOccurs=”unbounded” statement to allow an element to appear an unlimited number of times.

Example:

XML File: Myfriends.xml:

<?xml version="1.0" encoding="UTF-8"?>
 
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="friends.xsd">
 
<student>
  <full_name>David Jonas</full_name>
  <sibling_name>Tom Jonas</sibling_name>
</student>
 
<student>
  <full_name>Kia Smith</full_name>
  <sibling_name>Jenny Smith</sibling_name>
  <sibling_name>Drake Smith</sibling_name>
  <sibling_name>Robert Smith</sibling_name>
  <sibling_name>Tina Smith</sibling_name>
</student>
 
<student>
  <full_name>Stefan Gilbert</full_name>
</student>
 
</students>

Explanation:

In the above example, the XML file contains a root element named “students”. Three “student” elements are defined inside the root element, where each “student” element must contain a “full_name” element and it can contain up to five “sibling_name” elements.

Schema File: “friends.xsd”:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
 
<xs:element name="students">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="student" maxOccurs="unbounded">
        <xs:complextype>
          <xs:sequence>
            <xs:element name="full_name" type="xs:string"></xs:element>
            <xs:element name="sibling_name" type="xs:string"
           minOccurs="0" maxOccurs="5"></xs:element>
          </xs:sequence>
        </xs:complextype>
      </xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>
 
</xs:schema>

Group Indicators:

To define related sets of elements, the group indicators are used.

Element Groups:

The group declaration is used to define the element groups.

Syntax:

<xs:group name="groupname">
...
</xs:group>

Inside the group declaration, we need to define an all, choice, or sequence element.

Example:

<xs:group name="studentgroup">
  <xs:sequence>
    <xs:element name="fullname" type="xs:string"></xs:element>
    <xs:element name="fathername" type="xs:string"></xs:element>
    <xs:element name="birthday" type="xs:date"></xs:element>
  </xs:sequence>
</xs:group>

Explanation:

In the above example, we are defining a group named “studentgroup”. This group again defines a group of elements that must occur in an exact sequence.

Example:

<xs:group name="studentgroup">
  <xs:sequence>
    <xs:element name="fullname" type="xs:string"></xs:element>
    <xs:element name="fathername" type="xs:string"></xs:element>
    <xs:element name="birthday" type="xs:date"></xs:element>
  </xs:sequence>
</xs:group>
 
<xs:element name="student" type="studentinfo"></xs:element>
 
<xs:complextype name="studentinfo">
  <xs:sequence>
    <xs:group ref="studentgroup"></xs:group>
    <xs:element name="country" type="xs:string"></xs:element>
  </xs:sequence>
</xs:complextype>

Explanation:

In the above example, we are referencing the defined group in another definition.

Attribute Groups:

The attributeGroup declaration is used to define the attribute groups.

Syntax:

<xs:attributegroup name="groupname">
...
</xs:attributegroup>

Example:

<xs:attributegroup name="studentattrgroup">
  <xs:attribute name="fullname" type="xs:string"></xs:attribute>
  <xs:attribute name="fathername" type="xs:string"></xs:attribute>
  <xs:attribute name="birthday" type="xs:date"></xs:attribute>
</xs:attributegroup>

Explanation:

In the above example, we are defining an attribute group named “studentattrgroup”.

Example:

<xs:attributegroup name="studentattrgroup">
   <xs:attribute name="fullname" type="xs:string"></xs:attribute>
  <xs:attribute name="fathername" type="xs:string"></xs:attribute>
  <xs:attribute name="birthday" type="xs:date"></xs:attribute>
</xs:attributegroup>
 
<xs:element name="student">
  <xs:complextype>
    <xs:attributegroup ref="studentattrgroup"></xs:attributegroup>
  </xs:complextype>
</xs:element>

Explanation:

In the above example, we are referencing the defined attribute group in another definition.

Please Share