XSD Example

A schema can be written in different ways.

An XML Document:

employee.xml:

<?xml version="1.0" encoding="UTF-8"?>
 
<employee employeeid="122334"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
  <name>Jonas Javis</name>
  <department>
    <departmentname>IT</departmentname>
    <address>22 B Road</address>
    <city>Banglore</city>
    <country>India</country>
  </department>
  <experience>
    <title>Software Developer</title>
    <note>Well Experienced</note>
    <year>10</year>
    <grade>10</grade>
  </experience>
  <experience>
    <title>Team Manager</title>
    <note>Leader</note>
    <year>5</year>
    <grade>9</grade>
  </experience>
</employee>

Explanation:

In the above XML document, the root element, “employee” contains a required attribute called “employeeid”. There are three different child elements: “name”, “department” and “experience”, of the “employee” element. The “experience” element contains a “title”, an optional “note” element, a “year”, and a “grade” element. It appears twice. To tell the XML parser that this document should be validated against a schema, we included the line: xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” and to specify where the schema resides, the line: xsi:noNamespaceSchemaLocation=”employee.xsd” is included.

Create an XML Schema:

To create a schema for the XML document above, we will start by opening a new file named “employee.xsd” and will simply follow the structure in the XML document to define each element as we find it. Before including the xs:schema element to define a schema, the standard XML declaration should be included.

Example:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>

Explanation:

In the above schema, the standard namespace (xs) is used. The URI associated with this namespace is the Schema language definition. It has the standard value of http://www.w3.org/2001/XMLSchema.

Now, we will define the “employee” element as a complex type, because of the presence of an attribute and other elements. Here, we will use the xs:sequence element to surround the child elements of the “employee” element, in order to define an ordered sequence of sub-elements.

Example:

<xs:element name="employee">
  <xs:complextype>
    <xs:sequence>
     ...
    </xs:sequence>
  </xs:complextype>
</xs:element>

The “name” element does not contain any attributes or other elements and will thus be defined as a simple type. The namespace prefix associated with XML Schema that indicates a predefined schema data type is the one with which the type (xs:string) is prefixed.

Example:

<xs:element name="name" type="xs:string"></xs:element>

The elements “department” and “experience” are of the complex type. Let’s define the “department” element first.

Example:

<xs:element name="department">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="departmentname" type="xs:string"></xs:element>
      <xs:element name="address" type="xs:string"></xs:element>
      <xs:element name="city" type="xs:string"></xs:element>
      <xs:element name="country" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

To define the number of possible occurrences for an element, we can use the maxOccurs and minOccurs attributes. The maximum number of occurrences for an element is specified by maxOccurs while the minimum number of occurrences for an element is specified by minOccurs, both having a default value of 1. To define the “experience” element to appear multiple times inside an “employee” element, we will set the maxOccurs attribute of the “experience” element to “unbounded”. Thus, there can be as many occurrences of the “experience” element as the author wishes. Also, the “note” element here is specified to be optional by setting the minOccurs attribute to zero.

Example:

<xs:element name="experience" maxOccurs="unbounded">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="title" type="xs:string"></xs:element>
      <xs:element name="note" type="xs:string" minOccurs="0"></xs:element>
      <xs:element name="year" type="xs:positiveInteger"></xs:element>
      <xs:element name="grade" type="xs:decimal"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>

Now to declare the attribute of the “employee” element, we will specify use=”required”, since it is a required attribute. We must always declare the attribute at last.

Example:

<xs:attribute name="employeeid" type="xs:string" use="required"></xs:attribute>

employee.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<xs:element name="employee">
  <xs:complextype>
    <xs:sequence>
      <xs:element name="name" type="xs:string"></xs:element>
      <xs:element name="department">
        <xs:complextype>
          <xs:sequence>
            <xs:element name="departmentname" type="xs:string"></xs:element>
            <xs:element name="address" type="xs:string"></xs:element>
            <xs:element name="city" type="xs:string"></xs:element>
            <xs:element name="country" type="xs:string"></xs:element>
          </xs:sequence>
        </xs:complextype>
      </xs:element>
      <xs:element name="experience" maxOccurs="unbounded">
        <xs:complextype>
          <xs:sequence>
            <xs:element name="title" type="xs:string"></xs:element>
            <xs:element name="note" type="xs:string" minOccurs="0"></xs:element>
            <xs:element name="year" type="xs:positiveInteger"></xs:element>
            <xs:element name="grade" type="xs:decimal"></xs:element>
          </xs:sequence>
        </xs:complextype>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="employeeid" type="xs:string" use="required"></xs:attribute>
  </xs:complextype>
</xs:element>
 
</xs:schema>

Divide the Schema:

When documents are complex, the above design method can be difficult to read and maintain, however, it is simple. We will define all elements and attributes first and then refer to them using the ref attribute in the next design method.

New Design: employee.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<!-- definition of simple elements -->
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="departmentname" type="xs:string"></xs:element>
<xs:element name="address" type="xs:string"></xs:element>
<xs:element name="city" type="xs:string"></xs:element>
<xs:element name="country" type="xs:string"></xs:element>
<xs:element name="title" type="xs:string"></xs:element>
<xs:element name="note" type="xs:string"></xs:element>
<xs:element name="year" type="xs:positiveInteger"></xs:element>
<xs:element name="grade" type="xs:decimal"></xs:element>
 
<!-- definition of attributes -->
<xs:attribute name="employeeid" type="xs:string"></xs:attribute>
 
<!-- definition of complex elements -->
<xs:element name="department">
  <xs:complextype>
    <xs:sequence>
      <xs:element ref="departmentname"></xs:element>
      <xs:element ref="address"></xs:element>
      <xs:element ref="city"></xs:element>
      <xs:element ref="country"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>
 
<xs:element name="experience">
  <xs:complextype>
    <xs:sequence>
      <xs:element ref="title"></xs:element>
      <xs:element ref="note" minOccurs="0"></xs:element>
      <xs:element ref="year"></xs:element>
      <xs:element ref="grade"></xs:element>
    </xs:sequence>
  </xs:complextype>
</xs:element>
 
<xs:element name="employee">
  <xs:complextype>
    <xs:sequence>
      <xs:element ref="name"></xs:element>
      <xs:element ref="department"></xs:element>
      <xs:element ref="experience" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
    <xs:attribute ref="employeeid" use="required"></xs:attribute>
  </xs:complextype>
</xs:element>
 
</xs:schema>

Using Named Types:

We will define the classes or types, to enable us to reuse the element definitions in this design method, by naming the simpleTypes and complexTypes elements and then pointing to them through the type attribute of the element.

Third Design: employee.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<xs:simpletype name="stringtype">
  <xs:restriction base="xs:string"></xs:restriction>
</xs:simpletype>
 
<xs:simpletype name="inttype">
  <xs:restriction base="xs:positiveInteger"></xs:restriction>
</xs:simpletype>
 
<xs:simpletype name="dectype">
  <xs:restriction base="xs:decimal"></xs:restriction>
</xs:simpletype>
 
<xs:simpletype name="employeeidtype">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{8}"></xs:pattern>
  </xs:restriction>
</xs:simpletype>
 
<xs:complextype name="departmenttype">
  <xs:sequence>
    <xs:element name="departmentname" type="stringtype"></xs:element>
    <xs:element name="address" type="stringtype"></xs:element>
    <xs:element name="city" type="stringtype"></xs:element>
    <xs:element name="country" type="stringtype"></xs:element>
  </xs:sequence>
</xs:complextype>
 
<xs:complextype name="experiencetype">
  <xs:sequence>
    <xs:element name="title" type="stringtype"></xs:element>
    <xs:element name="note" type="stringtype" minOccurs="0"></xs:element>
    <xs:element name="year" type="inttype"></xs:element>
    <xs:element name="grade" type="dectype"></xs:element>
  </xs:sequence>
</xs:complextype>
 
<xs:complextype name="employeetype">
  <xs:sequence>
    <xs:element name="name" type="stringtype"></xs:element>
    <xs:element name="department" type="shiptotype"></xs:element>
    <xs:element name="experience" maxOccurs="unbounded" type="itemtype"></xs:element>
  </xs:sequence>
  <xs:attribute name="employeeid" type="orderidtype" use="required"></xs:attribute>
</xs:complextype>
 
<xs:element name="employee" type="employeetype"></xs:element>
 
</xs:schema>

Explanation:

In the above example, we are indicating that the data type is derived from a W3C XML Schema namespace datatype, by using the restriction element. The <xs:restriction base=”xs:string”> fragment implies that the value of the element or attribute must be a string value. Usually, to apply restrictions to elements, the restriction element is used. The below lines indicates that the value of the element or attribute must be a string. Also, the value of the element or attribute must be exactly eight characters in a row, and those characters must be a number from 0 to 9.

<xs:simpletype name="employeeidtype">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{8}"></xs:pattern>
  </xs:restriction>
</xs:simpletype>
Please Share