3.XML Schema(指南翻译)
XML文档能和一份DTD或XML Schema文件相关联。
3.1一个XML文档示例
请看下面这个名为"note.xml"的XML文档:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
3.2一个DTD文档
下面是一个DTD文件,名为"note.dtd",其中定义了上文"note.xml"中的元素(elements)。
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
第一行定义的note元素包含四个子元素:"to, from, heading, body" 。2至5行定义了to, from, heading, body 元素的类型(type)为"#PCDATA"。
3.3一个XML Schema
下面的例子是一个XML Schema文件,名为"note.xsd"。其中定义了上文"note.xml"中的元素。
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
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>
note元素是复合类型(complex type),因为它包含了其它元素。其余的元素(to, from, heading, body)是简单类型(simple types),因为它们没有包含别的元素。
3.4一个与DTD相关联的XML文档
下面的XML文档和上文给出的DTD相关联:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
3.5一个与XML Schema相关联的XML文档
下面的XML文档和上文给出的XML Schema相关联:
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
4.XSD-<schema>元素
<schema>元素是每个XML Schema的根元素。
4.1<schema>元素
<schema>元素是每个XML Schema的根元素:
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
<schema>元素可以包含一些属性。一个schema声明通常如下所示:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
下面的片段:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
表明此schema中使用的元素和数据类型来自于"http://www.w3.org/2001/XMLSchema"名称空间(namespace)。它同样指出来自于"http://www.w3.org/2001/XMLSchema"名称空间的元素和数据类型必须使用带"xs: "前缀。
此片段:
targetNamespace="http://www.w3schools.com"
表明此schema (note, to, from, heading, body.)定义的元素来自于"http://www.w3schools.com"名称空间。
此片段:
xmlns="http://www.w3schools.com"
表明默认名称空间为"http://www.w3schools.com"。
此片段:
elementFormDefault="qualified"
表明由这份schema声明的XML实例文档中用到的任何元素必须是有效的名称空间(namespace qualified)。
4.2在一份XML文档里提到Schema
此XML文档中包含一个XML Schema声明:
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
以下片段:
xmlns="http://www.w3schools.com"
指定了默认的名称空间(default namespace)声明。此声明告诉schema-校验器,此XML文档中使用的所有元素都在"http://www.w3schools.com"名称空间中声明。
一旦你有了可以利用的XML Schema Instance的名称空间(namespace):
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
你就可以使用SchemaLocation的属性。此属性有两个值。第一个值是你使用的名称空间。第二个值是在此名称空间中所使用的XML schema的位置:
xsi:schemaLocation="http://www.w3schools.com note.xsd"
5.XSD中的简单元素(Simple Elements)
XML Schemas定义了你在XML文件中使用的元素(elements)。一个简单元素只包含文本(text)。它不包含其它元素或属性。
5.1什么是简单元素?
一个简单元素只包含文本。它不包含其它元素或属性。
然而,“只包含文本”的定义限制(restriction)容易产生歧义。文本可以有许多不同的类型。它可以是XML Schema定义的诸多类型(boolean, string, date等)中的一种,或是你自己定义的一种类型。
你同样可以在一个数据类型中增加限制条件(restrictions)来限制它的内容,或者你可以要求数据与指定的式样相匹配。
5.2定义一个简单元素
定义一个简单元素的语法为:
<xs:element name="xxx" type="yyy"/>
xxx表示元素名,yyy表示此元素的数据类型。
XML Schema有许多内置的(built-in)数据类型。常用的类型有:
◆xs:string
◆xs:decimal
◆xs:integer
◆xs:boolean
◆xs:date
◆xs:time
5.3实例
这里有一些XML元素:
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>
这里是与之对应的简单元素定义:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
5.4简单元素的默认(default)值与固定(fixed)值
简单元素可以定义一个默认值或固定值。
默认值是指在没有定义值的情况下,自动的分配给此元素的值。
在下面的例子中,默认值为"red":
<xs:element name="color" type="xs:string" default="red"/>
固定值是指自动分配给此元素的值,并且你无法为其定义其它的值。
在下面的例子中,固定值为"red":
<xs:element name="color" type="xs:string" fixed="red"/>
6.XSD属性(Attributes)
所有的属性都声明为简单类型(simple types)。
6.1什么是属性?
简单元素没有属性。如果一个元素含有属性,那么它被认为是复合元素。但是属性本身总是被声明为简单类型的。
6.2如何定义属性?
定义属性的语法为:
<xs:attribute name="xxx" type="yyy"/>
xxx为属性名,yyy指定属性的数据类型。
XML Schema有许多内置的(built-in)数据类型。常用的类型有:
◆xs:string
◆xs:decimal
◆xs:integer
◆xs:boolean
◆xs:date
◆xs:time
6.3实例
这里有一个含有属性的XML元素:
<lastname lang="EN">Smith</lastname>
这里是与之对应得属性定义:
<xs:attribute name="lang" type="xs:string"/>
6.4属性的默认值与固定值
属性可以定义一个默认值或固定值。
默认值是指在没有定义值的情况下,自动的分配给此属性的值。
在下面的例子中,默认值为"EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
固定值是指自动分配给此属性的值,并且你无法为其定义其它的值。
在下面的例子中,固定值为"EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
6.5任意(Optional)和必须(Required)属性
默认时属性是任意的,要指明属性是必需的,须用到"use"属性:
<xs:attribute name="lang" type="xs:string" use="required"/>
6.6对内容的约束
当XML元素或属性有了已定义的数据类型,元素或属性的内容会有约束。
如果一个XML元素是"xs:date"类型,并包含着像"Hello World"的字符串,元素就不会进行检验。
你也可以用XML Schema给XML元素和属性添加约束限制。这些约束称为“面(facet)”。
7.XSD约束/面(facet)
约束用于给XML元素或属性定义可接受的值,关于对XML元素的约束称之为“面(facet)”。
7.1对单个值的约束
下面的例子给叫做"age"的元件定义了一个“约束(restriction)”。“age”的值要大等于0,小等于120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
7.2对一组值的约束
为了限制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"元素的一部分。
7.3对一系列值的约束
为了限制XML元件的内容以定义一系列可被使用的数字或字母,我们可以用“式样约束(pattern constraints)”。
下面的例子给叫做"letter"的元素定义可约束。唯一符合条件的值是 a到z之间的一个小写字母:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
接下来的例子给叫做"initials"的元素定义了一个约束。唯一符合条件的值是a到z之间的3个大写字母:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子给叫做"initials"的元素定义了一个约束。唯一符合条件的值是 a到z之间的三个大写或小写字母:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子给叫做"choice"的元素定义了一个约束,唯一符合条件的值是x,y,z三个字母中的任意一个:
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子给叫做"prodid"的元素定义了一个约束,唯一符合条件的值是0到9的5个阿拉伯数字的排列:<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
7.4对一系列值的其他约束
下面的例子给叫做"letter"的元素定义了一个约束。唯一符合条件的值是a 到z的小写字母(可以有多个)或0:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子也给叫做"letter"的元素定义了一个约束。唯一符合条件的值是一对或多对字母,每对都是一个小写字母后跟一个大写字母组成。举个例子,"sToP"在这种式样里是有效正确的,但"Stop" ,"STOP" 或 "stop"就都不是了:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子也给叫做"password"的元素定义了一个约束。一行里必须有8个字符,字符必须是a到z大或小写字母,或者是0到9的数字:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
7.5对空白符的约束
为了指定空白符该怎样被处理,我们可以用空白符约束。
下面的例子给叫做"address"的元素定义了一个约束。空白符设为"preserve"(保留),这意味着XML处理器不会删除任何空白字符:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子也给叫做"address"的元素定义了一个约束。空白符设为" replace "(替代),这意味着XML处理器会用空格替代所有的空白字符(换行符, 制表符, 空格符, 回车符):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子也给叫做"address"的元素定义了一个约束。空白符设为"collapse"(消除),这意味着XML处理器会清除所有的空白字符(换行符, 制表符, 空格符以及回车符都被空格符代替。头尾空格会被清除,多个空格也会减少为一个)
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
7.6对长度的约束
为了限制元素的长度值,我们会用length, maxLength, 和 minLength 约束。
下面的例子给叫做"password"的元素定义了一个约束。值必须正好有8个字符:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下面的例子给叫做"password"的元素定义了一个约束。值最少要有5个字符,最多有8个字符:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
| 约束 | 说明 |
| enumeration | 定义了一系列的有效值 |
| fractionDigits | 指定了允许的小数位数的最多位数。必须大于等于0 |
| length | 指定了允许的字符或列表项的个数。必须大于等于0 |
| maxExclusive | 指定了数值的上限(数值要比这个值小) |
| maxInclusive | 指定了数值上限(数值必须小于等于这个值) |
| maxLength | 指定了所允许的字符或列表项的最多个数。必须大于等于0 |
| minExclusive | 指定了数值的下限 (数值要比这个值小) |
| minInclusive | 指定了数值的下限(数值必须大于等于这个值) |
| minLength | 指定了所允许的字符或列表的最少个数。必须等于大于0个 |
| pattern | 定义了符合要求的字符的确切排列顺序 |
| totalDigits | 指定了所允许的字符的确切个数。必须大于0 |
| whiteSpace | 指定了空白该怎样被处理(换行符,制表符,空格符和回车符) |