XML Schema Definition,XSD

XML Schema是基于XML的DTD替代者

XML Schema 可描述XML文档的结构

XML Schema使用XML语法,好处:

  • 可使用 XML 编辑器来编辑 Schema 文件
  • 可使用 XML 解析器来解析 Schema 文件
  • 可通过 XML DOM 来处理 Schema
  • 可通过 XSLT 来转换 Schema(把数据转换成另外一种格式来表达)

XML Schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace ="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

代码解释:

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

 显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema" 。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs

targetNamespace="http://www.w3school.com.cn"

 显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

xmlns="http://www.w3school.com.cn"

 指出默认的命名空间是 "http://www.w3school.com.cn"。

elementFormDefault="qualified"

 指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

一个XML文件对 XML Schema 的引用

<?xml version="1.0"?>
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation=" http://www.w3school.com.cn note.xsd ">

<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
 

代码解释:

xmlns="http://www.w3school.com.cn"

 规定了默认命名空间的声明。此声明会告知 schema 验证器 ,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。

一旦您拥有了可用的 XML Schema 实例命名空间:

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

 您就可以使用 schemaLocation 属性了 。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:

xsi: schemaLocation ="http://www.w3school.com.cn note.xsd"
 

XSD 简易元素

    这是一些XML元素:

<lastname>Smith</lastname>
<age>28</age>
<dateborn>1980-03-27</dateborn>

   这是相应的建议元素定义:

<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>

  XSD 属性

     这是带有属性的 XML 元素:

<lastname lang="EN">Smith</lastname>

     这是对应的属性定义:

<xs:attribute name="lang" type="xs:string"/>

属性的默认值和固定值和属性必选

<xs:attribute name="lang" type="xs:string" default="EN"/>
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
<xs:attribute name="lang" type="xs:string" use="required"/>

对一组值的限定

如需把 XML 元素的内容限制为一组可接受的值,我们要使用枚举约束(enumeration constraint)

下面的例子定义 了带有一个限定的名为 "car" 的元素可接受的值只有 :Audi, Golf, BMW:

<xs:element name="car">

<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>

</xs:element>

 上面的例子也可以被写为:

< xs:element name="car" type="carType"/ >

<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>

注释 在这种情况下,类型 "carType" 可被其他元素使用,因为它不是 "car" 元素的组成部分。

一个 XSD 实例

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 >George Bush</orderperson>
< shipto >
<name>John Adams</name>
<address>Oxford Street</address>
<city>London</city>
<country>UK</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>

创建一个 XML Schema

这是这个名为 "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>
  

分割 Schema

接下来介绍的设计方法基于首先对所有元素和属性的定义,然后再使用 ref 属性来引用它们。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- 简易元素的定义 -->
<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"/>

<!-- 属性的定义 -->
<xs:attribute name="orderid" type="xs:string"/>

<!-- 复合元素的定义 -->
<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>
 

使用指定的类型(Named Types)

 第三种设计方法定义了类或者类型,这样使我们有能力重复使用元素的定义。具体的方式是:首先对简易元素和复合元素进行命名,然后通过元素的 type 属性来指向它们。

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

猜你喜欢

转载自applepieone.iteye.com/blog/1273109