Introducing XML Schema

In this example the XSD reference is included at the start of the XML document. You may find the XML Schema Part 0: Primer useful.

Schema

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
 <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="pop" type="xs:decimal"/>
   </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>

XML input

<country
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="country.xsd">
  <name>France</name>
  <pop>59.7</pop>
 </country>

Specifying child nodes

We can use the complexType structure to specify which nodes are permitted.

Rules to be enforced

The sequence can contain elements. Each element node may have minOccurs set.

Schema

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
 <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="capital" type="xs:string"/>
    <xs:element name="king" type="xs:string"
           minOccurs="0" maxOccurs="1"/>
    <xs:element name="queen" type="xs:string"
           minOccurs="0" maxOccurs="1"/>
   </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>

Valid input

<country>
  <name>United Kingdom</name>
  <capital>London</capital>
  <queen>Elizabeth II</queen>
</country>
<country>
  <name>United Kingdom</name>
  <capital>London</capital>
  <queen>Elizabeth II</queen>
</country>
<country>
  <name>United Kingdom</name>
  <capital>London</capital>
  <queen>Elizabeth II</queen>
</country>

Invalid input

<country>
  <name>Ruritania</name>
  <capital>Strelsau</capital>
  <king>Rudolf</king>
  <king>Michael</king>
  <king>Rudolf</king>
  <!--Should not allow three kings -->
</country>
<country>
  <name>Ruritania</name>
  <king>Rudolf</king>
  <!-- capital is missing-->
</country>
<country>
  <capital>Strelsau</capital>
  <!-- name is missing -->
</country>

Allowing options

The choice element can be used to permit options.

We wish to enforce the following rules:

Schema

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="name" type="xs:string"/>
    <xs:choice>
     <xs:element name="president" type="xs:string"/>
     <xs:element name="monarch" type="xs:string"/>
    </xs:choice>
  </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>

Valid input

<country>
  <name>United Kingdom</name>
  <monarch>Elizabeth II</monarch>
</country>
<country>
  <name>France</name>
  <president>Sarkozy</president>
</country>
<country>
  <name>Norway</name>
  <monarch>Harald</monarch>
</country>

Invalid input

<country>
  <name>Ruritania</name>
  <monarch>Rudolf</monarch>
  <monarch>Michael</monarch>
  <monarch>Rudolf</monarch>
  <!--Should not allow three monarchs -->
</country>
<country>
  <name>Ruritania</name>
  <president>Robert</president>
  <monarch>Rudolf</monarch>
  <!-- Cannot have president and monarch-->
</country>
<country>
  <name>Ruritania</name>
  <!-- Should have president or monarch-->
</country>

minOccurs and maxOccurs

We can specify the minimum and the maximum number of occurances. We can set maxOccurs to unbounded to an umlimited number of occurances.

Rule to be enforced:

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="module">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="name" type="xs:string"/>
   <xs:element name="teacher" type="xs:string"
      maxOccurs="unbounded"/>
   <xs:element name="prerequisite" type="xs:string"
      minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>

Valid input

<module>
  <name>XML 3</name>
  <teacher>Andrew</teacher>
  <prerequisite>Programming 2</prerequisite>
</module>
<module>
  <name>Database 2</name>
  <teacher>Andrew</teacher>
  <teacher>Ken</teacher>
</module>
<module>
  <name>XML4</name>
  <teacher>Ken</teacher>
  <prerequisite>XML 3</prerequisite>
  <prerequisite>Web Scripts 3</prerequisite>
</module>

Invalid input

<module>
  <name>XML5</name>
  <!-- no teacher -->
  <prerequisite>XML 3</prerequisite>
  <prerequisite>Web Scripts 3</prerequisite>
</module>
<module>
  <name>XML3</name>
  <name>IML3</name>
  <!-- Two names -->
  <teacher>Ken</teacher>
  <prerequisite>XML 3</prerequisite>
  <prerequisite>Web Scripts 3</prerequisite>
</module>
<module>
  <name>XML3</name>
  <teacher>Andrew</teacher>
  <room>A17</room>
  <!-- no such node allowed -->
</module>

maxOccurs on choice and sequence

We can use maxOccurs and minOccurs in choice and sequence elements. The expression <xsd:choice maxOccurs="unbounded"> allows the contents to be repeated one or more times. The expression <xs:element name="X" maxOccurs="unbounded"> allows the single element to be repeated one or more times.

Rule to be enforced:

Schema

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
 <xs:complexType>
  <xs:choice>
   <xs:element name="state" type="xs:string"
       maxOccurs="unbounded"/>
   <xs:choice maxOccurs="unbounded">
    <xs:element name="county" type="xs:string" />
    <xs:element name="borough" type="xs:string" />
   </xs:choice>
  </xs:choice>
 </xs:complexType>
</xs:element>
</xs:schema>

Valid input

<country>
  <!-- United kingdom -->
  <county>Kent</county>
  <county>Essex</county>
  <borough>Coventry</borough>
</country>
<country>
  <!-- USA -->
  <state>Ohio</state>
  <state>Oklahoma</state>
</country>
<country>
  <!-- United kingdom -->
  <county>Kent</county>
  <county>Essex</county>
</country>

Invalid input

<country>
  <state>Airstrip 1</state>
  <county>Borsetshire</county>
  <!-- May not mix state and county -->
</country>
<country>
  <state>England</state>
  <borough>Coventry</borough>
  <!-- May not mix state and borough-->
</country>
<country>
  <!-- Must have something -->
</country>