XSD Composite Type Indicator

Through indicators, we can control the way elements are used in the document.

Indicator

There are seven indicators:

Order indicator:

  • All
  • Choice
  • Sequence

Occurrence indicator:

  • maxOccurs
  • minOccurs

Group indicator:

  • Group name
  • attributeGroup name

Order Indicator

The Order indicator is 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: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>

Note:When using the <all> indicator, you can set <minOccurs> to 0 or 1, and only set the <maxOccurs> indicator to 1 (minOccurs and maxOccurs will be explained later).

Choice Indicator

The <choice> indicator specifies that a child element can appear or another child element can appear (either/or):

<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>

Tip:To set the child element to appear any number of times, set <maxOccurs> (to be explained later) to unbounded (infinite times).

Sequence Indicator

The <sequence> specifies that 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 Indicator

The Occurrence indicator is 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 specifies the maximum number of times an element can appear:

<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>

The above example indicates that the child element "child_name" can appear in the "person" element at least once (where the default value of minOccurs is 1) and at most 10 times.

minOccurs Indicator

The <minOccurs> indicator specifies the minimum number of times an element can appear:

<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" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

The above example indicates that the child element "child_name" can appear in the "person" element at least 0 times and at most 10 times.

Tip:To make the occurrence of an element unrestricted, please use the declaration maxOccurs="unbounded":

A real example:

The XML file named "Myfamily.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Tony Smith</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>David Smith</full_name>
<child_name>Jogn</child_name>
<child_name>mike</child_name>
<child_name>kyle</child_name>
<child_name>mary</child_name>
</person>
<person>
<full_name>Michael Smith</full_name>
</person>
</persons>

The above XML file contains a root element named "persons". Within this root element, we define three "person" elements. Each "person" element must contain a "full_name" element, and it can also contain up to 5 "child_name" elements.

This is the schema file "family.xsd":

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 indicator

The Group indicator is used to define related batch elements.

Element group

An element group is defined through the group declaration:

<xs:group name="组名称">
  ...
</xs:group>

You must define an all, choice, or sequence element within the group declaration. The following example defines a group named "persongroup", which defines a set of elements that must appear in exact order:

<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>

After you have finished defining the group, you can refer to it in another definition:

<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 group

An attribute group is defined through the attributeGroup declaration:

<xs:attributeGroup name="组名称">
  ...
</xs:attributeGroup>

The following example defines an attribute group named "personattrgroup":

<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>

After you have finished defining the attribute group, you can refer to it in another definition, like this:

<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>