Latest web development tutorials

XML Schema instance

This section will show you how to write an XML Schema. You will also learn different ways to write the schema.


XML documents

Let's look at this, called "shiporder.xml" XML document:

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>

The above XML document root element "shiporder", which contains the attribute must be named "orderid" of. "Shiporder" element contains three different child elements: "orderperson", "shipto" and "item". "Item" element appears twice, and it contains a "title", an optional "note" element, a "quantity" and a "price" elements.

The above line xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance", told a schema based on the XML parser to validate this document. The line: xsi: noNamespaceSchemaLocation = "shiporder.xsd" schema defines the position (in this case, it "shiporder.xml" in the same folder).


Create an XML Schema

Now, we need to create a schema for the XML document above.

We can start by opening a new file and this file is named "shiporder.xsd". To create a schema, we just need to simply follow the XML document structure, each element of the definition of what we found. First, we begin to define a standard XML declaration:

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

In the above schema, we use the standard namespace (xs), URI space associated with this name is the Schema language definition of (Schema language definition), its standard value is http://www.w3.org/ 2001 / XMLSchema.

Next, we need to define "shiporder" element. This element has a property that contains other elements, therefore we identified it as a complex type. Sub-element "shiporder" element is xs: sequence element surrounded defines the order of sub-elements:

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>

Then we need to "orderperson" element is defined as a simple type (because it does not contain any attributes or other elements). Type (xs: string) is prefixed by the namespace prefix provisions of this namespace and instructions predefined schema data types XML schema associated with:

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

Next, I need the two elements defined as complex type: "shipto" and "item". We start by defining "shipto" element begins:

<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

By schema, we can define the number of times an element can occur using maxOccurs and minOccurs attributes. maxOccurs define a maximum number of elements appear, and minOccurs defines the minimum number of occurrences of an element. Default maxOccurs and minOccurs is 1!

Now, we can define the "item" element of. This element can appear multiple times inside the "shiporder" element. This is done by the "item" element maxOccurs attribute value is set to "unbounded" to achieve, so that "item" element can be the creator appears as many times as desired. Please note, "note" element is optional. We have minOccurs attribute of this element is set to 0 on:

<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>

We can now declare "shiporder" attribute of the element. As this is a mandatory attribute, we specify use = "required".

Note: This property must be declared on the last:

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

This is a list of documents, called "shiporder.xsd" the schema file:

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

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>


Split Schema

In front of the design method is very easy, but it is difficult to read and maintain documents are complex Shique.

Next, based on the design method described in the first definition of all elements and attributes, and then use the ref attribute to refer to them.

This new approach is designed schema file ( "shiporder.xsd"):

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

<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>

</xs:schema>


Using the specified type (Named Types)

The third design method defines a class or type, so that we have the ability to define reusable elements. Specific approach is: first element of simple and complex elements name, and then type attribute of the element to point to them

This third method is the use of design schema file ( "shiporder.xsd"):

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

<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>

<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>

<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>

<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>

<xs:element name="shiporder" type="shipordertype"/>

</xs:schema>

restriction element showing the type of data derived from the W3C XML Schema namespace datatype. Therefore, the following fragment means that the value of the element or attribute value must be a string type:

<xs:restriction base="xs:string">

restriction element often used to impose restrictions to the elements. Consider the following fragment from the above schema:

<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>

This code indicates that the value of the element or attribute must be a string, and must be consecutive six characters, and these characters must be 0-9.