xml 学习笔记 3.xml Schema

XML Scema:XML模式语言,用来描述XML结构、数据内容、相关约束等方面特征的语言。

与dtd相比,XML Scema的优点:一致性,XML Scema是一种XML文档,从而使XML模式与实例定义达到统一;完备性,XML Scema引入数据类型、命名空间,并且支持对其他XML Scema的引用;规范性和准确性,XML Scema使用更加规范和完备的机制来约束XML文档;面向对象:XML Scema使用了面向对象的机制,使应用起来更加灵活;扩展性,XML Scema提供一些扩展机制(open),允许在事先无法准确描述数据模式的情况下,在 XML 实例数据中根据需要添加相关的数据。


xsd:annotation 元素来增加注释内容,xsd:annotation 元素可以包含两个子元素 xs:appinfo 和 xs:documentation。前者用于表示计算机应用程序处理的注释,而后者则表示供开发人员阅读的注释。


XML Scema中的数据类型实际上主要是针对XML元素而言的。数据类型为两类:简单类型 和 复杂类型。数据类型可以是有名的全局的数据类型,也可以是局部的无名的数据类型。

简单类型:

不包含任何子元素和属性的元素。也就是只包含文本内容或为空元素。

XML Schema中常用的基础类型有:

string string 数据类型的取值可以是任意字符串
normalizedString normalizedString 数据类型派生于 string 数据类型。normalizedString 数据类型的值可以包含任意字符,但是 XML 解析器将删除其中的 LF、CR 和制表符等空白字符;换句话说,normalizedString 数据类型是不包含上述特殊字符的字符串。
token token 数据类型也是 string 数据类型的派生类型,其中可以包含任意字符,但是 XML 解析器将删除其中的 LF、CR 和制表符等空白字符、开头和结尾的空格、以及连续的空格。
language 包含合法语言 id 的字符串。
Name 包含合法 XML 名称的字符串,可以包含命名空间前缀。
NCName 包含合法 XML 名称的字符串,不可以包含命名空间前缀。
ID、IDREFS、NMTOKEN 等 这些数据类型来自于 DTD,在 XML Schema 中保留了这些数据类型,并且与 DTD 中的使用方式相同,这些类型只能用于元素的属性。

float IEEE 的单精度 32 位浮点数。
decimal 可以使用十进制数字表示的实数。
integer 派生于 decimal,限制条件是十进制整数。
long 派生于 integer,限制条件是最大值为 9223372036854775807、最小值为 -9223372036854775808。
int 派生于 long,限制条件是最大值为 2147483647、最小值为 -2147483648。
nonPositiveInteger 派生于 integer,限制条件是最大值为 0。


date 用于指定一个日期,具体格式为:YYYY-MM-DD,其中:YYYY 表示年;MM 表示月;DD 表示日。假如 <xsd:element name="start" type="xsd:date"/>,那么该元素可以为 <start>2002-09-24</start>。
time 用于指定一个时间,具体格式为:hh:mm:ss,其中:hh 表示时;mm 表示分;ss 表示秒。假如 <xsd:element name="start" type="xsd:time"/>,那么该元素可以为 <start>09:00:00</start>。
datetime 用于指定一个日期和时间,具体格式为:YYYY-MM-DDThh:mm:ss。假如 <xsd:element name="startdate" type="xsd:dateTime"/>,那么该元素可以为 <startdate>2002-05-30T09:00:00</startdate>。
duration 指定一个时间间隔,具体格式为:PnYnMnDTnHnMnS,其中:P 表示时间间隔(必需的);nY 表示年数;nM 表示月数;依次类推。

boolean 用于指定 true 或者 false。合法的取值包括:true、false、1(表示 true)、0(表示 false)。
base64Binary、hexBinary 用于表示二进制格式的数据,base64Binary 表示 Base64 编码的二进制数据,hexBinary 表示十六进制编码的二进制数据。
anyURI 用于表示一个 URI,如果其中包括空格,必须使用 %20 进行替换。


自定义的简单类型可以通过限制、列表或者组合来构造新的类型。

限制 restriction:

<xsd:simpleType> 
    <xsd:restriction base="BaseType"> 
    ... facets descriptions ...
    </xsd:restriction> 
</xsd:simpleType>
base表示由什么数据类型进行的派生。

限制的方面有一下几种限制:

enumeration 定义一组合法的取值。
fractionDigits 指定最大的小数位数,必须大于或等于零。
length 指定字符串中字符或列表数据类型中项的数目,必须大于或等于零。
maxExclusive 指定数值类型值的上限(取值必须小于这个上限)。
maxInclusive 指定数值类型值的上限(取值必须小于或等于这个上限)。
maxLength 指定字符串中字符或列表数据类型中项的最大数目,必须大于或等于零。
minExclusive 指定数值类型值的下限(取值必须大于这个下限)。
minInclusive 指定数值类型值的下限(取值必须大于或等于这个下限)。
minLength 指定字符串中字符或列表数据类型中项的最小数目,必须大于或等于零。
pattern 指定一个正则表达式,描述合法的字符序列。
totalDigits 指定最大的位数,必须大于或等于零。
whiteSpace 指定如何处理空白字符(CR、LF、Space 和 Tab)。

列表List:

使用列表,列出列表中要求的数据类型。

可以是<xsd:list>....</xsd:list>中添加要求列表的数据类型,也可以是<xsd:list itemtype = " ... "/>直接调用全局的数据类型进行列表。


合并Union:

列表中只能对同一种数据类型进行存放。若有多种数据类型,则使用union。具体操作与List中类似


复杂类型:

包含子元素或属性的元素,而属性的声明包含在复杂类型的定义中。


包含属性的元素:

<xsd:complexType name="ComplexType">
	<xsd:attribute name="Att1Name" type="someSimpleType1"/>
	......
</xsd:complexType> 
在其中对属性的类型进行约束。

包含元素的元素:

XML Schema中有三种容器,以不同的出现顺序来包含子元素。子元素必须包含在容器中。

xsd:sequence: 表示序列,即元素必须按照声明顺序依次出现。

xsd:choice 表示选择,即从中选取一个元素出现。

xsd:all 表示所包含的内容全部需要出现,但是不分先后顺序。


包含属性和文本内容的元素:

使用simpleContent来表示文本内容或文本内容加属性。

<xsd:element name="ele_name">
  <xsd:complexType>
    <xsd:simpleContent>
         ....
    </xsd:simpleContent>
  </xsd:complexType>
</xsd:element>
其中也可使用xsd:extension或xsd:restriction来限制文本内容的类型时,只能base=""基础属性,xsd:extension中只能继续对属性进行限制。


包含文本属性和子元素

必须使用mixed属性来表明其包含的是混合内容

<xsd:complexType mixed="true">
        .....
    </xsd:complexType>



simpleType中有:

final 属性的值表示该数据类型不允许进行的操作的列表,#all 表示不能对该类型进行任何操作。或者是list | union | restriction

面向对象的特性:
complexType中的属性有:

abstract 属性表示该复杂类型是一个虚类型,只能用作其他类型的父类型。

final (#all | List of (extension | restriction)) 不能以all或者extenson或者 restriction的方式派生子类型

block 限制使用多态性,取值可能为 #all、extension、restriction。extension 和 restriction,分别表示禁止使用任何通过扩展/限制而派生的子类型的实例来代替声明为父类的元素。

其中final表示不能派生子类型,而block是不能使用子类型来对声明为父类的元素 实例化。

多态性的演示:

XML Schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="book">
		<xs:annotation>
			<xs:documentation>Comment describing your root element</xs:documentation>
		</xs:annotation>
		<xs:complexType>
			<xs:sequence>
				<xs:element name="title" type="titleType"/>
				<xs:element name="author" maxOccurs="3"/>
				<xs:element name="section" type="sectionType" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="sectionType">
		<xs:sequence>
			<xs:element name="title" type="titleType"/>
			<xs:choice minOccurs="0" maxOccurs="unbounded">
				<xs:element name="p"/>
				<xs:element name="figure">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="title" type="titleType"/>
							<xs:element name="image">
								<xs:complexType>
									<xs:attribute name="source" use="required"/>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
						<xs:attribute name="width" use="required"/>
						<xs:attribute name="height" use="required"/>
					</xs:complexType>
				</xs:element>
				<xs:element name="section" type="sectionType"/>
			</xs:choice>
		</xs:sequence>
		<xs:attribute name="id"/>
		<xs:attribute name="difficulty"/>
	</xs:complexType>
	<xs:simpleType name="titleType">
		<xs:restriction base="xs:string">
			<xs:minLength value="1"/>
			<xs:maxLength value="100"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="bookTitleType">
		<xs:restriction base="titleType">
			<xs:pattern value="(Data On the Web|TCP/IP Illustrated)"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="sectionTitleType">
		<xs:restriction base="titleType">
			<xs:pattern value="([1-9][0-9]*(\-[1-9][0-9]*)?\..*)"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="figureTitleType">
		<xs:restriction base="titleType">
			<xs:pattern value="([1-9][0-9]*\-[1-9][0-9]*(\-[1-9][0-9]*)?\..*)"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

对其中的titleType使用运行时多态。

<?xml version="1.0" encoding="UTF-8"?>
<!--用XMLSpy v2012产生的 XML文件(http://www.altova.com)-->
<book xsi:noNamespaceSchemaLocation="book.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<title xsi:type="bookTitleType">Data On the Web</title>
	<author>text</author>
	<author>text</author>
	<author>text</author>
	<section id="text" difficulty="text">
		<title xsi:type="sectionTitleType">1.xxxx</title>
		<p>text</p>
	</section>
	<section id="text" difficulty="text">
		<title xsi:type="sectionTitleType">1-1.xxxx</title>
		<p>text</p>
	</section>
	<section id="text" difficulty="text">
		<title>a</title>
		<figure width="12" height="24">
			<title xsi:type="figureTitleType">1-1-1.xxxx</title>
			<image source=""></image>
		</figure>
	</section>
	<section id="text" difficulty="text">
		<title xsi:type="sectionTitleType">120.250.215uafghaoh</title>
		<section>
			<title>afas</title>
			<figure width="" height="">
				<title xsi:type="figureTitleType">1-1-1.xxxx</title>
				<image source=""></image>
			</figure>
		</section>
	</section>
</book>

XML Schema中element其他的一些常用属性:

default = string 缺省值
fixed = string 固定值
id = ID id
maxOccurs = (nonNegativeInteger | unbounded) : 1 最大出现数
minOccurs = nonNegativeInteger : 1 最小出现数
name = NCName 名称
nillable = boolean : false 为false时,表示不能为空
ref = QName 引用某个已定义的全局元素
substitutionGroup = QName 替换组,使用当前元素来替换Qname代表的元素
type = QName 类型为。


对于xsd:element元素有unique、key、keyref来约束其为唯一值,键 和 键引用。

key的一个实例:

<xs:key name="spyIDkey">
			<xs:selector xpath="spyTable/row"/>
			<xs:field xpath="@spyID"/>
		</xs:key>
其中xs:selector 通过xpath选择一个元素集,是xs:field选择的属性或者元素或者其他值在 这个元素集为唯一值。unique的操作类似。

keyref多了一个refer,表示引用的key的名称:

<xs:keyref name="mrefkey" refer="spyIDkey">
			<xs:selector xpath="missionTable/row"/>
			<xs:field xpath="spyREF"/>
		</xs:keyref>

下面为完整的XML Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="info">
		<xs:annotation>
			<xs:documentation>Comment describing your root element</xs:documentation>
		</xs:annotation>
		<xs:complexType>
			<xs:sequence>
				<xs:element name="aliasTable">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="row" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="spyREF" type="xs:IDREF"/>
										<xs:element name="alias" type="xs:string"/>
									</xs:sequence>
									<xs:attribute name="aID" type="xs:int" use="required"/>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="missionTable">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="row" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="spyREF" type="xs:IDREF"/>
										<xs:element name="date" type="mydate"/>
										<xs:element name="description" type="xs:string"/>
										<xs:element name="status" type="statusType"/>
									</xs:sequence>
									<xs:attribute name="mID" type="xs:ID" use="required"/>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
				<xs:element name="spyTable">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="row" maxOccurs="unbounded">
								<xs:complexType>
									<xs:sequence>
										<xs:element name="firstName"/>
										<xs:element name="lastName"/>
									</xs:sequence>
									<xs:attribute name="spyID" type="xs:ID" use="required"/>
								</xs:complexType>
							</xs:element>
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
		<xs:key name="spyIDkey">
			<xs:selector xpath="spyTable/row"/>
			<xs:field xpath="@spyID"/>
		</xs:key>
		<xs:keyref name="mrefkey" refer="spyIDkey">
			<xs:selector xpath="missionTable/row"/>
			<xs:field xpath="spyREF"/>
		</xs:keyref>
		<xs:key name="mIDkey">
			<xs:selector xpath="missionTable/row"/>
			<xs:field xpath="@mID"/>
		</xs:key>
		<xs:key name="aIDkey">
			<xs:selector xpath="aliasTable/row"/>
			<xs:field xpath="@aID"/>
		</xs:key>
		<xs:keyref name="aREF" refer="spyIDkey">
			<xs:selector xpath="aliasTable/row"/>
			<xs:field xpath="spyREF"/>
		</xs:keyref>
	</xs:element>
	<xs:simpleType name="statusType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Mission Accomplished"/>
			<xs:enumeration value="In Progress"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="mydate">
		<xs:restriction base="xs:string">
			<xs:pattern value="([1-9][0-9]{3}\-([1-9]|1[0-2])\-([1-9]|[1-2][0-9]|3(0|1)))"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>



另一个直接用xmlspy转换得到的XML Schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
	<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
	<xs:element name="info">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="row" minOccurs="0" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="row">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="firstName"/>
				<xs:element ref="lastName"/>
				<xs:element ref="alias" minOccurs="0" maxOccurs="unbounded"/>
				<xs:element ref="mission" minOccurs="0" maxOccurs="unbounded"/>
			</xs:sequence>
			<xs:attribute name="spyID" type="xs:ID" use="required"/>
		</xs:complexType>
	</xs:element>
	<xs:element name="firstName">
		<xs:complexType mixed="true"/>
	</xs:element>
	<xs:element name="lastName">
		<xs:complexType mixed="true"/>
	</xs:element>
	<xs:element name="alias">
		<xs:complexType mixed="true"/>
	</xs:element>
	<xs:element name="mission">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="date"/>
				<xs:element ref="description"/>
				<xs:element ref="status"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="date">
		<xs:complexType mixed="true"/>
	</xs:element>
	<xs:element name="description">
		<xs:complexType mixed="true"/>
	</xs:element>
	<xs:element name="status">
		<xs:complexType mixed="true"/>
	</xs:element>
</xs:schema>

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念