Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML Schema Instance

XSD Instance


This section will show you how to write an XML Schema. You will also learn about different ways of writing a schema.


The XML Document

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="shiporder.xsd">

  John Smith

 

    Ola Nordmann

   

Langgt 23

    4000 Stavanger

    Norway

 

 

    Empire Burlesque

    Special Edition

    1

    10.90

 

 

    Hide your heart

    1

    9.90

 

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

The line above, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", tells the XML parser to validate this document against a schema. This line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies the location of the schema (here, it is in the same folder as "shiporder.xml").


Creating an XML Schema

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

We can start by opening a new file and naming it "shiporder.xsd". To create the schema, we simply need to follow the structure in the XML document and define each element we find. First, we start by defining a standard XML declaration:

...

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

Next, we need to define the "shiporder" element. This element has an attribute and contains other elements, so we identify it as a complex type. The child elements of the "shiporder" element are surrounded by the xs:sequence element, which defines the order of the child elements:

  xs:complexType

    xs:sequence

      ...

   

 

Then we need to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The prefix of the type (xs:string) is specified by the namespace prefix associated with the XML schema that indicates the predefined schema data types:

Next, we need to define two elements as complex types: "shipto" and "item". We start by defining the "shipto" element:

  xs:complexType

    xs:sequence

     

     

     

     

   

 

With the schema, we can use the maxOccurs and minOccurs attributes to define how many times an element can occur. maxOccurs defines the maximum number of times an element can occur, while minOccurs defines the minimum number of times. The default values of both maxOccurs and minOccurs are 1!

Now we can define the "item" element. This element can occur multiple times inside the "shiporder" element. This is achieved by setting the value of the maxOccurs attribute of the "item" element to "unbounded", so that the "item" element can occur as many times as the author wishes. Note that the "note" element is optional. We have set its minOccurs attribute to 0:

  xs:complexType

    xs:sequence

     

     

     

     

   

 

Now we can declare the attribute of the "shiporder" element. Since this is a required attribute, we specify use="required".

**Note:** The declaration of this attribute must be placed last:

This is the listing of the schema file named "shiporder.xsd":

  xs:complexType

    xs:sequence

     

     

        xs:complexType

          xs:sequence

           

           

           

           

         

       

     

     

        xs:complexType

          xs:sequence

           

           

           

           

         

       

     

   

   

 


Splitting the Schema

The previous design method is very simple, but when a document is complex it becomes difficult to read and maintain.

The design method introduced next is based on first defining all elements and attributes, and then using the ref attribute to reference them.

This is the schema file designed with the new method ("shiporder.xsd"):

  xs:complexType

    xs:sequence

     

     

     

     

   

 

  xs:complexType

    xs:sequence

     

     

     

     

   

 

  xs:complexType

    xs:sequence

     

     

     

   

   

 


Using Named Types

The third design method defines classes or types, which gives us the ability to reuse element definitions. Specifically: first name the simple and complex elements, then point to them through the type attribute of the elements

This is the schema file designed using the third method ("shiporder.xsd"):

 

 

 

 

   

 

  xs:sequence

   

   

   

   

 

  xs:sequence

   

   

   

   

 

  xs:sequence

   

   

   

 

 

The restriction element shows that the data type is derived from a data type in the W3C XML Schema namespace. Therefore, the following fragment means that the value of an element or attribute must be of string type:

The restriction element is often used to impose restrictions on an element. Look at the following fragments from the schema above:

 

   

 

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