XSD 实例

This chapter will demonstrate how to write an XML Schema. You will also learn that a schema can be written in different ways.
这章将示范如何写一份XML Schema.你也会了解到可以使用不同的方法书写schema。


An XML Document
一份XML文档

Let's have a look at this XML document called "shiporder.xml":
让我们看这份名为"shiporder.xml"的XML文档

<?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 XML document above consists of a root element, "shiporder", that contains a required attribute called "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.
上述XML文档有根目录元素"shiporder",它有个必需的属性叫做"orderid","shiporder"元素包含了三个不同的子元素:"orderperson", "shipto" 和 "item"."item"元素出现了两次,它包含了一个"title"元素,一个任意的"note"元素,一个"quantity"元素和一个"price"元素。

The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line:
xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").
上面的一行xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",告诉了XML解析器这个文档应该被一份schema检验。xsi:noNamespaceSchemaLocation="shiporder.xsd"指定了schema应该所在位置(这里它以"shiporder.xml"文件待在相同的文件夹里)。


Create an XML Schema
创建一篇XML Schema

Now we want to create a schema for the XML document above.
现在我们想要为上面的XML文档创建一份schema

We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:
开始先打开叫做"shiporder.xsd"的新文件。为创建新的schema,我们可以简单地按照XML文档的结构,每当发现一个元素时就进行定义。一开始先作标准的XML声明,接下来是定义了schema的xs:schema元素:

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...


</xs:schema>

In the schema above we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, which has the standard value of http://www.w3.org/2001/XMLSchema.
在上述schema里我们用了标准的名称空间(xs),并且和这个名称空间相联系的URI是Schema语言定义,它有个标准值http://www.w3.org/2001/XMLSchema.

Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:
接下来,我们必须定义"shiporder”元素。这个元素有个属性,并包含着其他的元素,因此我们把它看成是复合类型。"shiporder"元素的子元素由xs:sequence元素包围着,xs:sequence元素定义了子元素一定顺序的排列。

<xs:element name="shiporder">

 <xs:complexType>
  <xs:sequence>
  ...
  ...
  </xs:sequence>
  ...
 </xs:complexType>
</xs:element>

Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type:
那么我们必须将"orderperson"元件定义为简单类型(因为它不含有任何属性或其它元素)。种类用名称空间(namespace)的前缀使用与XML Schema相关的名称空间前缀化,这里的XML Schema指明了一个前缀的Schema数据类型。

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

Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining the "shipto" element:
接下来,我们必须定义两个复合类型的元素,"shipto" 和 "item"。先由定义"shipto"元素开始:

<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>

With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!
用schema,我们可以用maxOccurs(最多出现次数)和minOccurs(最少出现次数)属性来定义一个元素的可能出现次数。maxOccurs指定了一个元素的最多出现次数,minOccurs指定了一个元素的最少出现次数。maxOccurs
和minOccurs的默认值都是1。

Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minOccurs attribute to zero:
现在我们可以来定义"item"元素,这个元素可以在"shiporder"元素里重复出现。这可以通过设置"item"元素的maxOccurs属性为"unbounded"实现,属性为"unbounded"意味着"item"元素可以根据编者意愿重复出现多次。要注意"note"元素是任意的,我们可以通过设置minOccurs属性为0来实现。

<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 the attribute of the "shiporder" element. Since this is a required attribute we specify use="required".
我们现在可以声明"shiporder"元素的属性了。因为这是项必须属性,我们可以指定:use="required"。

Note: The attribute declarations must always come last:
注意:属性声明必须总是放在最后

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

Here is the complete listing of the schema file called "shiporder.xsd":
下面是"shiporder.xsd" schema文件的完整例子:

<?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>


Divide the Schema
划分Schema

The previous design method is very simple, but can be difficult to read and maintain when documents are complex.
上面的设计方法非常简单,但因为文件是复合(或复杂的,Complex)的,所以难于阅读和利用。

The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute.
下面的设计方法是:先定义所有的元素和属性,然后用ref属性引用它们。

Here is the new design of the schema file ("shiporder.xsd"):
下面是schema文件的新设计方式("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 Named Types
使用有名称的类型

The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element.
第三种设计方法定义了种类或类型,这使我们能重新用元素定义。通过给简单类型和复合类型元素命名,接着在元素的种类属性类型里指明它们的方法来做到这点。

Here is the third design of the schema file ("shiporder.xsd"):
这是schema文件("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>

The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So, the following fragment means that the value of the element or attribute must be a string value:
约束元素指出了这个数据类型是由一个W3C XML Schema名称空间数据类型派生出来的。所以,下面的片段意味着元素或属性的值必须是一个字符串的值

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

The restriction element is more often used to apply restrictions to elements. Look at the following lines from the schema above:
约束元素常常用于给元素添加约束。请看上述schema中的几行:

<xs:simpleType name="orderidtype">

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

This indicates that the value of the element or attribute must be a string, it must be exactly six characters in a row, and those characters must be a number from 0 to 9.
这指出了元素或属性的值必须是字符串,而且必须是一排6个从0到9之间的数字。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值