XML Schema(XSD)编程规范(一)

今天是2011/10/13日,下面我将详细的说明XML schema(XSD)的编写

1.xsd 是xml的扩展语言,它是一种格式约束,表示一种规则,是一种结构化的数据结构说明

 xsd是dtd 的继任者
  xsd有比dtd更加的灵活和多变
  xsd是基于xml编写的
  xsd支持数据结构,而且还可以直接到处对象
  xsd支持命名空间

xml schema 使用 XML 语法
由 XML 编写 XML Schema 有很多好处:
    不必学习新的语言
    可使用 XML 编辑器来编辑 Schema 文件
    可使用 XML 解析器来解析 Schema 文件
    可通过 XML DOM 来处理 Schema
    可通过 XSLT 来转换 Schema

2.下面我来演示一个小例子:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="note"
    targetNamespace="http://tempuri.org/note.xsd"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
	<xs:element name="note">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="note1" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
				<xs:element name="note2" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs: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://tempuri.org/note.xsd D:\C#\XML\XML\note.xsd">
	<note1>note1</note1>
	<note2>note2</note2>
</note>

 这两个xml和xml schema是对应的

以后我们将讲解为什么xml schema 会这样写

3.基本知识

xsd基本标签

 <xs:schema>

<?xml version="1.0"?>

<xs:schema>

...
...
</xs:schema>

 

<schema> 元素可包含属性。一个 schema 声明往往看上去类似这样:

 <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="note"
    targetNamespace="http://tempuri.org/note.xsd"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

...
...
</xs:schema>
xmlns:xs="http://www.w3.org/2001/XMLSchema"

显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:

targetNamespace="http://tempuri.org/note.xsd" 

显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://tempuri.org/note.xsd"。

xmlns="http://www.w3school.com.cn" 

指出默认的命名空间是 "http://www.w3school.com.cn"。

elementFormDefault="qualified" 

指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

您拥有了可用的 XML Schema 实例命名空间:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:

xsi:schemaLocation= "http://tempuri.org/note.xsd D:\C#\XML\XML\note.xsd"

 

<xs:element >

<xs:element name="name" type="xs:string" />

此处 name指元素的名称,xs:string 指元素的数据类型。XML Schema 拥有很多内建的数据类型。

 

上面是内置的数据类型结构表

 

 小例子:

<lastname>chen</lastname>
<age>24</age>
<dateborn>1980-03-27</dateborn>

相应的简易元素定义:

<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/> 

 元素的默认值和固定值

<xs:element name="color" type="xs:string" default="red"/

<xs:element name="color" type="xs:string" fixed="red"/>

 

<xs:attribute>

 这是带有属性的 XML 元素:

<lastname lang="EN">Smith</lastname>

这是对应的属性定义:

<xs:attribute name="lang" type="xs:string"/>

属性可拥有指定的默认值或固定值。

当没有其他的值被规定时,默认值就会自动分配给元素。

在下面的例子中,缺省值是 "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

固定值同样会自动分配给元素,并且您无法规定另外的值。

在下面的例子中,固定值是 "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

 

 

复合元素

复合元素,"product",是空的:

<product pid="1345"/>

复合元素,"employee",仅包含其他元素:

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

复合元素,"food",仅包含文本:

<food type="dessert">Ice cream</food>

复合元素,"description",包含元素和文本:

<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>

 

1.通过命名此元素,可直接对"employee"元素进行声明,就像这样:

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

2. "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="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" 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="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>

 

XSD限制

 

限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet。

 

1.对值的限定 

 

<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> 

 

 

2.对一组值的限定

 

<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>

3.对一系列的值的限定

下面的例子定义了带有一个限定的名为 "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 其中的三个:

<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> 

 

下一个例子定义了带有一个限定的名为 "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:

 

 

<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> 

下面的例子定义了带有一个限定的名为 "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> 

 

下面的例子定义了带有一个限定的名为"letter"的元素。可接受的值是一对或多对字母,每对字母由一个小写字母后跟一个大写字母组成。

 

<xs:element name="letter">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="([a-z][A-Z])+"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下面的例子定义了带有一个限定的名为 "gender" 的元素。可接受的值是 male 或者 female:

 

<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> 

 

4.对空白字符的限定

 

<xs:element name="address">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="preserve"/>
  </xs:restriction>
</xs:simpleType>

</xs:element>

这个例子也定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "replace",这意味着 XML 处理器将移除所有空白字符(换行、回车、空格以及制表符):

<xs:element name="address">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="replace"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

这个例子也定义了带有一个限定的名为 "address" 的元素。这个 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.对长度的限定

本例定义了带有一个限定且名为 "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 定义空白字符(换行、回车、空格以及制表符)的处理方式。

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值