This section shows you how to write a XML Schema. You’ll also learn different ways to write schema. Let’s take a look at this XML document called “shiporder.xml”: The above XML document includes the root element “shiporder”, which contains attributes that must be named “orderid”. The “shiporder” element contains three different child elements: “orderperson”, “shipto”, and “item”. The “item” element appears twice, and it contains a “title”, an optional “note” element, a “quantity”, and a “price” element. The above line, xmlns:xsi= “http://www.w3.org/2001/XMLSchema-instance”, tells the XML parser to validate the document against a schema. This line: xsi:noNamespaceSchemaLocation= “shiporder.xsd” specifies the location of schema (in this case, it is in the same folder as “shiporder.xml”). Now we need to create a schema for the above XML document. We can start by opening a new file and name it “shiporder.xsd”. To create a schema, we simply follow the structure in the XML document and define each element we find. Let’s start by defining a standard XML declaration: In the above schema, we used a standard namespace (xs). The URI associated with this namespace is the language definition of Schema (Schema language definition), and its standard value is http://www.w3.org/2001/XMLSchema . Next, we need to define the “shiporder” element. This element has an attribute that contains other elements, so we identify it as a compound type. the child elements of the “shiporder” element are surrounded by the xs:sequence element, defining the order of the child elements: Then we need to define the “orderperson” element as a simple type (this is because it does not contain any attributes or other elements). The prefix of the type (xs:string) is defined by the prefix of the namespace associated with the XML schema that indicates the predefined schema data type: Next, I need to define two elements as compound types: “shipto” and “item”. Let’s start by defining the “shipto” element: With 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 occurrences of an element, while minOccurs defines the minimum number of occurrences of an element. The default values for both maxOccurs and minOccurs are 1! Now we can define the “item” element. This element can appear 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 appear as many times as the creator wants. Note that the “note” element is optional. We have set the minOccurs attribute of this element to 0: Now we can declare the attributes of the “shiporder” element. Since this is a required attribute, we specify use= “required”. 注意: The declaration of this property must be placed at the end: This is a list of documents for the schema file named “shiporder.xsd”: The previous design method is very easy, but difficult to read and maintain when the document is complex. The design approach described next is based on the definition of all elements and attributes first, and then referencing them using the ref attribute. This is a schema file (“shiporder.xsd”) designed with a new method: The third design method defines a class or type, which gives us the ability to reuse the definition of the element. The specific way is to first name simple elements and composite elements, and then point to them through the element’s type attribute This is a schema file (“shiporder.xsd”) designed using the third method: The restriction element shows that the data type is derived from the W3C XML Schema namespace. Therefore, the following snippet means that the value of an element or attribute must be of type string: Restriction elements are often used to impose restrictions on elements. Take a look at the following snippets from the above schema: This code indicates that the value of an element or attribute must be a string and must be six consecutive characters, and these characters must be a number from 0 to 9.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>
Create a XML Schema ¶
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
</xs:schema>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
</xs:sequence>
</xs:complexType>
</xs:element>
<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:attribute name="orderid" type="xs:string" use="required"/>
<?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 ¶
<?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>
Use the specified type (Named Types) ¶
<?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>
<xs:restriction base="xs:string">
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>