当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 实例

Schema (XSD)
XSD 逻辑值数据类型
XML Schema总结
XML Schema 参考
XSD 验证

Schema (XSD) 中的 XSD 实例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 767 ::
收藏到网摘: n/a

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"元素定义为简单类型,因为它不包含任何属性或其它元素。类型(xs:string)使用命名空间的前缀为前缀,该命名空间是与指定前缀Schema数据类型的XML 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.
上述设计方法非常简单,但是,因为文件是复合类型,所以难于阅读和维护。

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.
第三种设计方法定义了类或类型。通过为简单类型元素和复合类型元素命名,并通过元素的 type [ 类型 ] 属性指向这些元素,我们就能够重新使用元素定义。

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之的数字。

评论 (1) 1 All

登陆 | 还没注册?