XML Schema
概念
XML Schema(模式或架构)是用于定义XML文档的结构和内容的文档。
其作用与DTD文档一样。
使用Schema定义XML文档结构,并且以用它来验证XML文档的正确性,用来判断实例是否符合模式中所描述的所有约束。
主要检验如下内容:
验证数据的格式是否正确及是否超出值的范围
验证所有必需的信息是否都存在
为元素和属性添加默认值和固定值
和DTD相比:
DTD是用一种与 XML不同的语法编写的,而XML模式使用的是一种类XML的语言。
Schema支持丰富的数据类型。
Schema支持命名空间机制。
XML Schema语言有两种模型:
Microsoft开发的Microsoft XML Schema
和W3C开发的W3C XML Schema
。
Microsoft XML Schema已经开发成熟并运用到实际,而最为正式的XML Schema语言是由W3C指定的XML Schema规范,简称为xsd(XML Schema Definition)。xsd也提供了数据类型的支持和结构定义的方法。
基本结构
1.Microsoft XML Schema模式
Schema文件由一组元素组成,其根元素是Schema,其文件结构为:
<Schema name=“schema-name” xmlns=“namespace”>
……
</Schema>
一般命名空间为
xmlns="urn:schemas-microsoft-com:xml-data"
Xmlns:dt=“urn:schemas-microsoft-com:datatypes”
例子:
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data" >
<ElementType name="姓名" content="textOnly" model="closed"/>
<ElementType name="性别" content="textOnly" model="closed"/>
<ElementType name="出生日期" content="textOnly" model="closed"/>
<ElementType name="员工" content="mixed" model="closed" order=“seq">
<element type="姓名"/>
<element type="性别"/>
<element type="出生日期"/>
</ElementType>
</Schema>
====>>
<?xml version="1.0" ?>
<员工 xmlns="x-schema:4-1.xml">
<姓名>李亮</姓名>
<性别>男</性别>
<出生日期>1985.2.5</出生日期>
</员工>
2.W3C XML Schema模式
文档后缀名为.xsd ,其根元素是Schema,其文件结构为:
<Schema xmlns:xsd=“namespace”>
……
</Schema>
命名空间为
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
例子:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="员工">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="姓名" type="xsd:string"/>
<xsd:element name="性别" type="xsd:string"/>
<xsd:element name="出生日期" type="xsd:date"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
====>>
<?xml version="1.0" ?>
<员工 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="4-3.xsd">
<姓名>李亮</姓名>
<性别>男</性别>
<出生日期>1985-02-05</出生日期>
</员工>
MS XML Schema的数据类型
<Schema name=“mySchema”
xmlns=“urn:schemas-microsoft-com:xml-data”
xmlns:dt=“urn:schemas-microsoft-com:datatypes”>
……
</Schema>
Schema文件清单:
<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="职员" content="eltOnly" model="closed">
<AttributeType name="ID" dt:type="int"/>
<attribute type="ID"/>
<element type="姓名"/>
<element type="性别"/>
<element type="出生日期"/>
<element type="婚姻"/>
<element type="部门"/>
</ElementType>
<ElementType name="姓名" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="性别" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="出生日期" content="textOnly" dt:type="date"/>
<ElementType name="婚姻" content="textOnly" dt:type="boolean"/>
<ElementType name="部门" content="textOnly" dt:type="enumeration" dt:values="技术部 营销部 生产部"/>
</Schema>
====>>
<?xml version="1.0" ?>
<职员 xmlns="x-schema:4-5.xml" ID="001002">
<姓名>刘婷</姓名>
<性别>女</性别>
<出生日期>1982-05-08</出生日期>
<婚姻>false</婚姻>
<部门>营销部</部门>
</职员>
W3C XML Schema的数据类型
简单类型
元素中仅包含文本数据,且不包含属性。语法结构如下:
<xsd:simpleType name=名称 id=ID>
<xsd:restriction|xsd:list|xsd:union>
……
</xsd:simpleType>
原子类型
可以使用xsd:restriction限制现有的简单类型
<xsd:element name="membership_ID">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{5}(-\d{4})?"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name=“number” type=“MyInteger”/>
<xsd:simpleType name=“MyInteger“>
<xsd:restriction base=“xsd:integer“>
<xsd:minInclusive value=“10000“/>
<xsd:maxInclusive value=“99999“/>
</xsd:restriction>
</xsd:simpleType>
对应的有效的XML片段:
<number>10001</number>
类似的有:
minExclusive
maxExclusive
列表类型
形式:<xsd:list itemType=“元素类型”>
<xsd:element name=“score” type=“listofNumber”/>
<xsd:simpleType name=‘listofNumber’>
<xsd:list itemType=“xsd:decimal”>
</xsd:simpleType>
例:
<score>20 30 40 50</score>
联合类型
形式:<xsd:union memberType=“元素类型 元素类型” >
<xsd:simpleType name=“成绩”>
<xsd:union memberType=“学生 分数”>
</xsd:simpleType>
枚举类型
<xs:simpleType name=“gender">
<xs:restriction base="xs:string">
<xs:enumeration value="男"></xs:enumeration>
<xs:enumeration value="女"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
复杂类型
有四种复杂类型的元素:
第一种类型是“只含元素”类型,它只能含有子元素或属性,而不能含有文本。
第二种类型是“只含文本”类型,它只能含有文本和属性,而不能含有子元素。
第三种类型是“空元素”类型,它可以有属性,但不能含有子元素或文本。
第四种类型是“混合内容”类型,它可以包含元素,属性和文本的组合。
用complextType进行说明。
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="图书">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="书名" type="xsd:string"/>
<xsd:element name="作者" type="xsd:string"/>
<xsd:element name="售价" type="xsd:decimal"/>
<xsd:element name="简介" type="xsd:string"/>
<xsd:element name="出版信息" minOccurs="0" maxOccurs=“1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="出版社" type="xsd:string"/>
<xsd:element name="版次" type="xsd:date"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="isbn" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
====>>
<?xml version="1.0" ?>
<图书 isbn="7-111-10288-6">
<书名>C#技术内幕</书名>
<作者>Joseph Mayo</作者>
<售价>59.00</售价>
<简介>
这是一本关于C#编程语言的指南和参考书。
</简介>
<出版信息>
<出版社>清华大学出版社</出版社>
<版次>2003年1月</版次>
</出版信息>
</图书>
“只含文本”元素的声明
用simpleContent 。
simpleContent 元素包含对 complexType 元素(它以字符数据或 simpleType 元素为内容)的扩展或限制并且不包含任何子元素。
语法格式:
<xsd:simpleContent >
<xsd:restriction|xsd:extension>
……
</xsd:simpleContent>
例:
<xsd:element name=“member” type=“member_details_type”/>
<xsd:complexType name=“member_details_type”>
<xsd:simpleContent>
<xsd:extension base=“xsd:string”>
<xsd:attribute name=“family_name” type=“xsd:string”/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
对应的XML片段:
<member family_name=“zhang”>san</member>
“空元素”的声明
例:
<xsd:element name="price">
<xsd:complexType>
<xsd:attribute name="curr" type="xsd:string"/>
<xsd:attribute name="val" type="xsd:decimal"/>
</xsd:complexType>
</xsd:element>
对应的XML片段:
<price curr=“USD“ val=“69.95“ />
complexContent
可以从一个复杂类型派生出新的复杂类型,适用于包含属性和子元素。
语法格式:
<xsd:complexContent>
<xsd:restriction|xsd:extension>
……
</xsd:complexContent>
例:
<xs:element name="employee" type="fullpersoninfo"/>
<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:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
group、all、choice、sequence
(1)group 元素用于定义在复杂类型定义中使用的元素组。
语法格式:
<group name=名称 ref=组名 maxOccurs=数值 minOccurs=数值>
<all|choice|sequence>
</group>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="custGroup">
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="orderdetails" type="xs:string"/>
<xs:element name="billto" type="xs:string"/>
<xs:element name="shipto" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:element name="order" type="ordertype"/>
<xs:complexType name="ordertype">
<xs:group ref="custGroup"/>
<xs:attribute name="status" type="xs:string"/>
</xs:complexType>
</xs:schema>
(2)all 元素规定子元素能够以任意顺序出现。
<xs:element name="person">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
(3)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>
(4)sequence 要求组中的元素以指定的顺序出现在包含元素中。每个子元素只能出现1次。
<xs:element name="personinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" 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>
批注 语法:
<annotation id=ID any attributes >
<appinfo|documentation>*
</annotation>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:appinfo>W3School Note</xs:appinfo>
<xs:documentation >
This Schema defines a W3School note!
</xs:documentation>
</xs:annotation>
.
</xs:schema>
元素类型声明格式如下:
<ElementType name=“” content=“textOnly|eltOnly|mixed|empty” model=“open|closed” order=“one|seq|many” dt:type=“string|int|…”>
<attribute|AttributeType|element>……
</ElementType>
<?xml version="1.0" ?>
<Schema name="4-28.xml" xmlns="urn:schemas-microsoft-com:xml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="班级" content="eltOnly" order="many" model="closed">
<element type="教师" minOccurs="1" maxOccurs="*"/>
<element type="学生" minOccurs="1" maxOccurs="*"/>
</ElementType>
<ElementType name ="教师" content ="eltOnly" order="seq" model="closed">
<AttributeType name="教师号" dt:type="id" required="yes"/>
<attribute type="教师号"/>
<element type="教师姓名"/>
</ElementType>
<ElementType name="教师姓名" content="textOnly" model="closed" dt:type="string"/>
<ElementType name ="学生" content="eltOnly" order="seq" model="closed">
<AttributeType name="班主任" dt:type="idref"/>
<attribute type="班主任"/>
<element type="学生姓名"/>
<element type="学号"/>
<element type="性别"/>
<element type="出生日期" minOccurs="0" maxOccurs="1"/>
<element type="系别"/>
</ElementType>
...
元素声明格式如下:
<element name|type|ref|maxOccurs|minOccurs|fixed|…>……
<complexType|simpleType|annotation|……>
</element>
<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>
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="班级">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="教师" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="姓名" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="教师号" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="学生" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="姓名" type="xsd:string"/>
<xsd:element name=“学号“ type="xsd:unsignedShort"/>
<xsd:element name="性别" type="xsd:string"/>
<xsd:element name="出生日期" type="xsd:date“ minOccurs="0"/>
<xsd:element name="系别" type="系别列表"/>
</xsd:sequence>
<xsd:attribute name="班主任" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
空元素声明格式如下:
<xs:element name="product">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:integer">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
简单元素可拥有指定的默认值或固定值。
当没有其他的值被规定时,默认值就会自动分配给元素。
<xsd:element name="color" type="xsd:string" default="red"/>
<xsd:element name="color" type="xsd:string" fixed="red"/>
元素引用:
引用是利用element标记符的ref属性实现的。主要适用于避免在文档中多次定义同一个元素,应当将经常使用的元素定义为根元素的子元素,以便在文档的任何地方引用它。
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”user” type=”xsd:string” />
<xsd:element name=”name”>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref=”user” />
……
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
元素引用:
element元素的substitutionGroup属性为某个定义元素起一个别名
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”yonghu” type=”xsd:string” substitutionGroup=”user” />
<xsd:element name=”user” type=”xsd:string” />
<xsd:element name=”name”>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref=”user” />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
对应的XML文档
<?xml version=”1.0”?>
<name>
<user>string</user>
</name>
或者:
<?xml version=”1.0”?>
<name>
<yonghu>string</yonghu>
</name>
用元素AttributeType进行属性声明,而要指定某个元素拥有一个AttributeType元素,则应使用元素attribute。语法格式如下:
< AttributeType
name=“属性名”
dt:type=“属性类型”
dt:value=“枚举值列表” />
<attribute type=“属性名”
default=“缺省值”
required=“{yes|no}”/>
<AttributeType name="等级" dt:type="char" required="yes"/>
<AttributeType name="编号" dt:type="id" required="yes"/>
<ElementType name="商品" content="eltonly">
<attribute type="等级" default="A"/>
<attribute type="编号"/>
</ElementType>
用attribute 元素定义一个属性。语法格式如下:
<attribute default=string fixed=string form=qualified|unqualified
id=ID name=NCName ref=QName type=QName
use=optional|prohibited|required any attributes >
(annotation?,(simpleType?))
</attribute>
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
引用 Microsoft XML Schema:
<根元素名 xmlns=“x-schema:schema文档名” />
引用 W3C XML Schema:
(1). Schema文档中未定义目标命名空间(targetNamespace)
<员工 xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:noNamespaceSchemaLocation="4-3.xsd">
……
</员工>
(2). 如果Schema文档中已经定义目标命名空间(targetNamespace)
<员工 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation=“http://127.0.0.1/xml 4-3.xsd"
……
xmlns=“http://127.0.0.1/xml”>
……
</员工>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/2003/05/soap-envelope" xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/Customers"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
elementFormDefault="qualified">
<xs:import namespace="http://schemas.microsoft.com/dynamics/2006/02/documents/Customers"
schemaLocation="create_customer_response1.xsd"/>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element ref="createCustomersResponse"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element ref="soap:Body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
文件二(create_customer_response1.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/dynamics/2006/02/documents/Customers" xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey"
elementFormDefault="qualified">
<xs:element name="createCustomersResponse">
<xs:complexType>
<xs:sequence>
<xs:element ref="EntityKey"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>