XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 可描述 XML 文档的结构。
XML Schema 语言也可作为 XSD(XML Schema Definition)来被引用。
XML Schema 比 DTD 更强大。XML Schema 支持数据类型。
XML 编写 XML Schema 有很多好处:
1.不必学习新的语言
2.可使用 XML 编辑器来编辑 Schema 文件
3.可使用 XML 解析器来解析 Schema 文件
4.可通过 XML DOM 来处理 Schema
5.可通过 XSLT 来转换 Schema
二、案例XML 文档note.xml
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>小梅</to>
<from>羽婷</from>
<heading>语句</heading>
<body>XXXXX</body>
</note>
note.xml的DTD约束文件
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
note.xsd" 的 XML Schema 约束文件
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"<!--xmlns和targetNamespace地址一样 -->
elementFormDefault="qualified">
<!--elementFormDefault指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。xs规定了来自命名空间的前缀。 -->
<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>
对 DTD 的引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM
"http://www.w3school.com.cn/dtd/note.dtd">
<note>
<to>vvvv</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
对 XML Schema 的引用
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--<schema> 元素是每一个 XML Schema 的根元素。 -->
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
定义简易元素的语法:<xs:element name="color" type="xs:string" default="red"/>//默认值
<xs:element name="color" type="xs:string" fixed="red"/>//固定值
定义简易元素的语法:<xs:element name="color" type="xs:string" default="red"/>//默认值
<xs:element name="color" type="xs:string" fixed="red"/>//固定值
<xs:element name="lastname" type="xs:string"/><!--最常用的类型
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
-->
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XSD 属性
定义属性的语法是:<xs:attribute name="xxx" type="yyy"/>
属性的默认值和固定值
<xs:attribute name="lang" type="xs:string" default="EN"/>
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
可选的和必需的属性在缺省的情况下,属性是可选的。如需规定属性为必选,使用 "use" 属性
<xs:attribute name="lang" type="xs:string" use="required"/>
XSD 限定 / Facets
<xs:element name="age"><!--age 的值不能低于 0 或者高于 120-->
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/><!--使用枚举约束(enumeration constraint)-->
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/><!--使用模式约束(pattern constraint)-->
</xs:restriction>
</xs:simpleType>
</xs:element>
对空白字符的限定
whiteSpace 限定被设置为 "preserve"——> XML 处理器不会移除任何空白字符:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
whiteSpace 限定被设置为 "replace"——>XML处理器将移除所有空白字符(换行、回车、空格以及制表符):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
whiteSpace 限定被设置为 "collapse"——>XML处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
其值最小为 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>
复合元素,"food",仅包含文本:
<food type="dessert">Ice cream</food>
复合元素,"description",包含元素和文本:
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
定义复合元素:
" employee" 元素可以使用 type 属性,这个属性的作用是引用要使用的复合类型的名称:
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
定义无内容的类型就必须声明一个在其内容中只能包含元素的类型
<xs:element name="product">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:integer">
<xs:attribute name="prodid" type="string"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
声明了一个复合类型,其内容被定义为整数值,并且 "shoesize" 元素含有名为 "country" 的属性:
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
为了使字符数据可以出现在 "letter" 的子元素之间,mixed 属性必须被设置为 "true
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XSD类型指示器
对于所有的 "Order" 和 "Group" 指示器(any、all、choice、sequence、group name 以及 group reference),其中的 maxOccurs 以及 minOccurs 的默认值均为 1。如需使某个元素的出现次数不受限制,使用 maxOccurs="unbounded" .
All 指示器
<all> 指示器可规定子元素以何种顺序出现,以及每个子元素须出现几次:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
Choice 指示器
<choice> 指示器可规定可出现某个子元素或者可出现另外一个子元素(非此即彼):
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
Sequence 指示器
<xs:element name="person">
<xs:complexType>
<xs:sequence><!--规定子元素必须按照特定的顺序出现-->
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
把 group 定义完毕以后,就可以在另一个定义中引用它了
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
XSD <any> 元素
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XSD <anyAttribute> 元素
通过使用 <anyAttribute> 元素可以向 "person" 元素添加任意数量的属性:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
XSD 元素替换(Element Substitution)
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
阻止元素替换
为防止其他的元素替换某个指定的元素,请使用 block 属性
<xs:element name="name" type="xs:string" block="substitution"/>
在某个 schema 中规格化字符串数据类型的例子
<xs:element name="customer" type="xs:normalizedString"/>
schema 中日期时间声明的例子:
<xs:element name="startdate" type="xs:dateTime"/>
scheme 中十进制数声明的例子
<xs:element name="prize" type="xs:decimal"/>