XSD Example
This section will demonstrate how to write an XML Schema. You will also learn different methods for writing a schema.
XML Document
Let's look at this XML document named "shiporder.xml":
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 and contains a "title", an optional "note" element, a "quantity", and a "price" element.
The line xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser to validate this document against a schema. The 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 above XML document.
We can start by opening a new file and naming it "shiporder.xsd". To create the schema, we simply follow the structure of the XML document, defining each element we find. First, we begin with 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, with the standard value being 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 consider it a complex type. The child elements of "shiporder" are enclosed by the xs:sequence element, which defines the order of the child elements:
Then we need to define the "orderperson" element as a simple type (since it does not contain any attributes or other elements). The prefix of the type (xs:string) is determined by the prefix of the namespace, which is associated with the XML schema indicating predefined schema data types:
Next, we need to define two elements as complex types: "shipto" and "item". We start by defining the "shipto" element:
With the schema, we can use the maxOccurs and minOccurs attributes to define the number of times an element may appear. maxOccurs defines the maximum number of times an element can appear, while minOccurs defines the minimum number of times an element can appear. The default values for maxOccurs and minOccurs are both 1!
Now, we can define the "item" element. This element can appear multiple times within the "shiporder" element. This is achieved by setting the maxOccurs attribute of the "item" element to "unbounded", allowing the "item" element to appear as many times as the creator wishes. Note that the "note" element is optional. We have set the minOccurs attribute of this element to 0:
Now, we can declare the attribute for the "shiporder" element. Since this is a required attribute, we specify use="required".
Note: The declaration of this attribute must be placed last:
Here is the document listing for the schema file named "shiporder.xsd":
Splitting the Schema
The previous design method is very straightforward, but it can be difficult to read and maintain when the document is complex.
The next design method is based on first defining all elements and attributes, and then using the ref attribute to reference them.
Here is the schema file ("shiporder.xsd") designed with the new method:
Using Named Types
The third design method defines classes or types, allowing us to reuse element definitions. Specifically, we name simple and complex elements first, and then point to them using the type attribute of the element.
Here is the schema file ("shiporder.xsd") designed using the third method:
The restriction element indicates that the data type is derived from the W3C XML Schema namespace data type. Therefore, the following snippet means that the element or attribute's value must be a string:
The restriction element is often used to impose restrictions on elements. Consider these snippets from the schema above:
This code indicates that the element or attribute's value must be a string and must consist of exactly six consecutive characters, all of which must be digits between 0-9.