XML Schema

DTD的不足

语法结构问题:与XML语法不一致,不支持DOM、XPath、XSLT等
数据类型问题:有限的数据类型,不支持布尔、日期、时间等数据,不能扩展
文档结构问题:DTD中元素和属性是全局的,不是上下文相关的
名称空间问题:不支持名称空间

Schema的特点

Schema的优势
Schema是独立的XML文档,扩展名为.xsd
已经定义了丰富的数据类型,支持派生,支持格式约束
元素、属性具有上下文相关性
支持名称空间
Schema的不足
不能嵌入到XML文档中
语法较复杂
还不能定义属性和属性值的相关性,如若CPU的“厂商”为“Other”则必须在“描述”中声明
XML文档-》note.xml

<?xml version="1.0" encoding="GB2312"?> <note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me Forever!</body> </note>

DTD文档-》note.dtd

<?xml version="1.0" encoding="GB2312"?> <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

schema文档-》note.xsd

<?xml version="1.0" encoding="GB2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <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>
根元素schema

Schema文档的根元素schema的声明<?xml version="1.0" encoding="GB2312"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">Schema文档的根元素只能为schemaschema 中用到的元素和数据类型来自名称空间"http://www.w3.org/2001/XMLSchema”elementFormDefault和attributeFormDefault分别为qualified(限定的)和unqualified(非限定的),表明元素名称需要给出名称空间,而属性名不需要

名称空间的声明

Schema中对名称空间的声明:<xs:schema targetNamespace=“目标名称空间URL" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=“默认名称空间URL">targetNamespace(目标名称空间):是引用该Schema的XML文档的名称空间,即XML文档中元素、属性、数据类型等名称的有限范围,对应的是一个URL,可以不是一个文件标准名称空间:即定义了Schema标准的名称空间,包括schema、element、simpleType、string等关键字、内置数据类型和标准的Schema语法等默认名称空间:即省略了名称空间前缀的名称所属的空间(可以与目标空间不同)

名称空间的引用

在XML文档中引用Schema的名称空间:其实这个特点的好处就是可以为特定的命名空间绑定特定的schema,一般情形使用前者即可,简单
若Schema文档中没有定义目标名称空间,则<元素名称 xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“Schema文档URI">若Schema文档中已定义了目标名称空间,则<元素名称 xmlns=“默认空间URL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="目标空间URL Schema文档URI">
例如

<!--Schema文档(文件名为score.xsd)--> <?xml version="1.0" encoding="gb2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns=“http://127.0.0.1/xml” targetNamespace=“http://127.0.0.1/xml” elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="学生" type=“xs:string"/> </xs:schema> <!--XML文档--> <?xml version="1.0" encoding="GB2312"?> <学生 xmlns=http://127.0.0.1/xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://127.0.0.1/xml score.xsd">张绍康</学生>

元素和属性的声明
在根元素schema下定义顶层子元素(top level element),在某元素内定义其子元素和属性
元素声明
<xs:element name=名称 type=类型/>
属性声明
<xs:attribute name=名称 type=类型 use=次数/>

数据类型

Schema的数据类型分为简单类型、复杂类型两种
复杂类型complexType
复杂类型定义XML元素的结构关系,如包含属性、子元素、子元素间的关系等例如

<xs:complexType name="学生型"> <xs:sequence> <xs:element name="姓名" type="xs:string"/> <xs:element name="年龄" type="xs:int" minOccurs="0"/> </xs:sequence> <xs:attribute name="学号" type="xs:int"/> </xs:complexType>

Schema中可定义“学生型”元素:
<xs:element name=“学生” type=“学生型”/>
只有元素的内容可以是复杂类型的
简单类型simpleType
简单类型的数据是XML最小的结构单位例如

<xs:simpleType name=“编号型”> <xs:restriction base=“xs:int”/> </xs:simpleType>

定义了“编号型”数据类型,其基础类型为整型,因此
<xs:attribute name=“编号” type=“编号型”/>
<xs:element name=“编号” type=“编号型”/>
所定义的属性、元素的内容都必须是整型
若元素是简单类型,则不能包含子元素。对于属性而言,由于属性值不允许再分割,因此只能是简单类型
数据类型派生(Derivation)
与面向对象方法中类的派生相似,XML Schema支持数据类型的派生,即在已经定义的数据类型基础上,定义新的数据类型。可以在简单类型基础上派生新的简单类型或复杂类型,也可以在复杂类型基础上派生新的复杂类型。复杂类型的派生有扩展派生(extension)、约束派生(restriction)两种,简单类型的派生有扩展派生、约束派生、列表派生(list)、联合体派生(union)四种。最常见的派生是复杂类型的扩展派生、约束派生和简单类型的约束派生例如

<xs:simpleType name=“邮政编码”> <xs:restriction base=“xs:string”> <xs:pattern value=“\d{6}”/> </xs:restriction> </xs:simpleType>

Schema的内部数据类型

Schema内置了许多数据类型,包括字符串、数值、逻辑、日期时间、URI等,其中大部分是简单类型。默认类型可以是anyType或anySimpleType。
只有部分内置的数据类型属于原始类型(Primitive),即不能通过取值约束方式从其他数据类型派生出来的数据类型,这些数据类型包括:
字符型:string、QName、NOTATION
数值型:float、double、decimal逻辑型:boolean
日期时间型:date、time、duration、eYear、……
URI型:anyURI二进制:base64Binary、hexBinary

复杂类型complexType
complexType定义复杂数据类型,声明元素的属性、子元素、文本内容等
复杂数据类型可以是具名的,也可以是匿名的
具名复杂类型(必须有name属性)只能定义在schema元素下,是全局的
匿名复杂类型(不能有name属性)只能出现在element元素下,是局部的若complexType的父元素是schema或redifine,就必须有一个simpleContent或complexContent子元素。
若父元素是element,则可以包含attribute、group等子元素。
complexContent
complexContent是complexType的子元素,表示该complexType将显式地从其他complexType派生。
complexContent必须有一个子元素extension或restriction来扩展或约束complexType的定义
simpleContensimpleContent用于从simpleType派生complexType,表示该complexType只有文本内容,没有子元素

<?xml version="1.0" encoding="GB2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="文件系统"> <xs:complexType> <xs:complexContent> <xs:restriction base="目录类"> <xs:sequence> <xs:element name="目录" type="目录类" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="文件" type="文件类" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> <xs:attribute name="创建时间" type="xs:dateTime" use="prohibited"/> <!--不能出现在实例文档中 --> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> <xs:complexType name="目录类" final="extension"> <xs:complexContent> <xs:extension base="文件类"> <xs:sequence> <xs:element name="目录" type="目录类" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="文件" type="文件类" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="文件类"> <xs:attribute name="名称" type="文件名" use="required" /> <xs:attribute name="创建时间" type="xs:dateTime" /> </xs:complexType> <xs:simpleType name="文件名" final="#all"> <xs:restriction base="xs:token"> <xs:pattern value='[^|\\\*\?:"<>]+' /> </xs:restriction> </xs:simpleType> </xs:schema>

简单类型simpleType
simpleType定义从Schema已定义的简单类型再派生的简单类型,简单类型不能再包含任何元素或属性简单数据类型也可以分为具名的或匿名的具名简单类型有name属性,只能定义在schema元素下,是全局的匿名简单类型不能有name属性,可以出现在element、attribute、list、restriction、union元素下,是局部的complexType必须有一个子元素,可以是list、union或restriction之一
array.xsd:

<?xml version="1.0" encoding="GB2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="数组"> <xs:complexType> <xs:attribute name="数组元素" type="整数列表" use="required"/> <xs:attribute name="容量" type="整数和无穷" use="required"/> </xs:complexType> </xs:element> <xs:simpleType name="整数列表"> <xs:list itemType="整数和无穷"/> </xs:simpleType> <xs:simpleType name="整数和无穷"> <xs:union memberTypes="xs:integer"> <xs:simpleType> <xs:restriction base="xs:NMTOKEN"> <xs:enumeration value="无穷"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType> </xs:schema>

array.xml

<?xml version="1.0" encoding="UTF-8"?> <数组 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="array.xsd" 容量="3" 数组元素="1 -1 无穷"> </数组>

扩展entension

<xs:simpleType name="姓名"> <xs:restriction base="xs:token"> <xs:minLength value="2"/> </xs:restriction> </xs:simpleType> <xs:complexType name="人员"> <xs:simpleContent> <xs:extension base="姓名"> <xs:attribute name="性别" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType>

列表
list.xsd

<?xml version="1.0" encoding="GB2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="专家门诊"> <xs:complexType> <xs:sequence> <xs:element name="专科" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="名称" type="xs:token" use="required"/> <xs:attribute name="专家" type="姓名列表" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:simpleType name="姓名列表"> <xs:list> <xs:simpleType> <xs:restriction base="xs:token"> <xs:minLength value="2"/> </xs:restriction> </xs:simpleType> </xs:list> </xs:simpleType> </xs:schema><?xml version="1.0" encoding="UTF-8"?> <专家门诊 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="list.xsd"> <专科 专家="张三 李四 王五" 名称="内科" /> </专家门诊>

并union

<xs:simpleType name="成绩"> <xs:union> <xs:simpleType> <xs:restriction base="xs:NMTOKEN"> <xs:enumeration value="优秀"/> <xs:enumeration value="良好"/> <xs:enumeration value="中"/> <xs:enumeration value="及格"/> <xs:enumeration value="不及格"/> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:int"> <xs:minInclusive value="0"/> <xs:maxInclusive value="100"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType> <xs:attribute name=“成绩” type=“成绩”/> <学生 name=“张三” 成绩=“95”/> <学生 name=“李四” 成绩=“及格”/>

选择choice······

数据取值

Schema的优势之一是允许对数据的取值进行综合设置,可以定义数据的取值范围、输入和显示方式,等等。
取值约束
对简单类型,可以采用取值约束限制数据的取值:
长度限制:length、maxLength、minLength
大小限制:maxExclusive、minExclusive、maxInclusive、minInclusive
精度限制:fractionDigits、totalDigits
模式匹配:pattern(正则表达式)
空白处理:whiteSpace
枚举类型:enumeration

<xs:simpleType name=“邮政编码”> <xs:restriction base=“xs:string”> <xs:pattern value=“[0-9]+” fixed=“1”/> <xs:length value=“6” fixed=“true”/> </xs:restriction> </xs:simpleType> <xs:simpleType name=“技术职称”> <xs:restriction base=“xs:string”> <xs:enumeration value=“教授”/> <xs:enumeration value=“副教授”/> <xs:enumeration value=“讲师”/> <xs:enumeration value=“助教”/> <xs:enumeration value=“无”/> </xs:restriction> </xs:simpleType>

键key和键引用
keyrefKey元素定义键约束,键的值由一个或多个字段组成,在实例文档中,对于包含键的的元素,键值必须是唯一的已定义的键可以被keyref元素通过refer属性引用,以构成键引用约束在key元素中,从包含元素开始,用selector元素选出key的参考元素,在参考元素基础上,用field元素选出键值
keyref.xsd

<?xml version="1.0" encoding="GB2312"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="学生名单"> <xs:complexType> <xs:sequence> <xs:element name="班级" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="学生" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="姓名" type="xs:token"/> <xs:element name="学号" type="xs:token"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="班长"> <xs:complexType> <xs:attribute name="学号" type="xs:token" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:key name="学生学号"> <xs:selector xpath="学生"/> <xs:field xpath="学号"/> </xs:key> <xs:keyref name="班长学号" refer="学生学号"> <xs:selector xpath="班长"/> <xs:field xpath="@学号"/> </xs:keyref> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

keyref.xml

<?xml version="1.0" encoding="GB2312"?> <学生名单 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="keyref.xsd"> <班级> <学生> <姓名>张三</姓名> <学号>0001</学号> </学生> <学生> <姓名>李四</姓名> <学号>0002</学号> </学生> <班长 学号="0002"/> </班级> </学生名单>
外部文档

导入import所导入的Schema的目标名称空间(由根元素schema的targetNameSpace属性声明)不能与当前Schema的目标名称空间相同如果所导入的Schema没有目标名称空间,则其中的定义和说明都不会被导入包含include若要包含的Schema具有目标名称空间,则必须与当前Schema相同若没有声明目标名称空间,则所包含的声明和定义都与当前Schema合并为一个整体


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值