与DTD文件不同,Schema使用XML,同时Schema语言是设计用来替代DTD的。
下面就来介绍一下XML Schema一些基本概念:
本文所用到的XML文件为如下:
test1.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
<title>
<fontname>Helvetica</fontname>
<fontsize>36</fontsize>
</title>
<body>
<fontname>Helvetica</fontname>
<fontsize>36</fontsize>
</body>
<window>
<heigh>200</heigh>
<width>400</width>
</window>
<window>
<heigh>2200</heigh>
<width>4200</width>
</window>
<color>
<red>0</red>
<green>50</green>
<blue>100</blue>
</color>
<color>
<red>20</red>
<green>150</green>
<blue>100</blue>
</color>
<menu>
<item fontsize="12">Times Roman </item>
<item fontsize="12">Helvetica</item>
</menu>
<menu>
<item fontsize="12">Times Roma1n </item>
<item fontsize="12">Helvetic1a</item>
</menu>
</configuration>
如果要在文档中引用Schema文件,需要在XML文件当中的根元素中添加属性:
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xsi:noNamespaceSchemaLocation="test1.xsd"
<span style="white-space:pre"> </span>...
</configuration>
在XML Schema文件里面文件开头
<pre name="code" class="html"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/test1"
xmlns:tns="http://www.example.org/test1"
elementFormDefault="qualified">
对上面这段代码的理解:
xmlns是xml的命名空间,xs是命名空间的别名,同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs。
xs:schema就是限定名
schema就是本地名
targetNamespace显示被此 schema 定义的元素来自命名空间: "http://www.example.org/test1"。
elementFormDefault指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。
定义简易元素的语法:
<xs:element name="xxx" type="yyy"/>
比如:
<xs:element name="fontname" type="xs:string"></xs:element> <xs:element name="fontsize" type="xs:int"></xs:element>
定义属性的语法是:
<xs:attribute name="xxx" type="yyy"/>比如:<xs:attribute name="fontSize" type="xs:int"></xs:attribute>
定义xsd限定
下面的例子定义了带有一个限定且名为 "age" 的元素。age 的值不能低于 0 或者高于 120:
<xs:element name="age" type="ageType"></xs:element><xs:simpleType name="ageType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction></xs:simpleType>
复杂类型定义(下面是定义了test1.xml文件中的/configuration/menu/item的):
<pre name="code" class="html"><pre name="code" class="html"><xs:element name="configuration" type="confType"></xs:element>
<xs:element name="menu" type="menuType"></xs:element>
<xs:complexType name="confType">
<xs:sequence>
<xs:element ref="title" maxOccurs="10"></xs:element>
<xs:element ref="body" maxOccurs="10"></xs:element>
<xs:element ref="window" maxOccurs="10"></xs:element>
<xs:element ref="color" maxOccurs="10"></xs:element>
<xs:element ref="menu" maxOccurs="10"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="menuType">
<xs:sequence>
<xs:element ref="item" maxOccurs="10">
<xs:attribute ref="fontSize" use="required"></xs:attribute>
</xs:element>
</xs:sequence>
</xs:complexType>
关于xs:sequence就是在complexType中,必须出现一次且xs:sequence里面的元素需要按顺序出现。比如上面confType中xs:sequence里面有title,body,window,color,menu五个元素,这五个元素每个元素出现的最少次数是1,最大次数是10;可以出先的样式是 title+,body+,window+,color+,menu+;带加号就是至少出现1次。
关于xs:chioce就是在complexType中,多选一出现,选中的元素可以重复出现。
<xs:complexType name="confType">
<xs:chioce>
<xs:element ref="title" maxOccurs="10"></xs:element>
<xs:element ref="body" maxOccurs="10"></xs:element>
<xs:element ref="window" maxOccurs="10"></xs:element>
<xs:element ref="color" maxOccurs="10"></xs:element>
<xs:element ref="menu" maxOccurs="10"></xs:element>
</xs:chioce>
</xs:complexType>
这就是说上述的代码,五选一,假设是title,那么其他四个元素都不会出现,但是title至少出现1次,最多出现10次。
关于上面test1.xml文件,我们编写出的xsd文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/test1" xmlns:tns="http://www.example.org/test1" elementFormDefault="qualified">
<!-- 简单元素element定义 -->
<xs:element name="fontname" type="xs:string"></xs:element>
<xs:element name="fontsize" type="xs:int"></xs:element>
<xs:element name="heigh" type="xs:int"></xs:element>
<xs:element name="weight" type="xs:int"></xs:element>
<xs:element name="red" type="xs:int"></xs:element>
<xs:element name="blue" type="xs:int"></xs:element>
<xs:element name="green" type="xs:int"></xs:element>
<xs:element name="item" type="xs:string"></xs:element>
<!-- 简单属性Attribute定义 -->
<xs:attribute name="fontSize" type="xs:int"></xs:attribute>
<!-- 复杂类型定义 -->
<xs:complexType name="titleType">
<xs:sequence>
<xs:element ref="fontname"></xs:element>
<xs:element ref="fontsize"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bodyType">
<xs:sequence>
<xs:element ref="fontname"></xs:element>
<xs:element ref="fontsize"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="windowType">
<xs:sequence>
<xs:element ref="heigh"></xs:element>
<xs:element ref="weight"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="colorType">
<xs:sequence>
<xs:element ref="red"></xs:element>
<xs:element ref="green"></xs:element>
<xs:element ref="blue"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="menuType">
<xs:sequence>
<xs:element ref="item" maxOccurs="10">
<xs:attribute ref="fontSize" use="required"></xs:attribute>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="confType">
<xs:sequence>
<xs:element ref="title" maxOccurs="10"></xs:element>
<xs:element ref="body" maxOccurs="10"></xs:element>
<xs:element ref="window" maxOccurs="10"></xs:element>
<xs:element ref="color" maxOccurs="10"></xs:element>
<xs:element ref="menu" maxOccurs="10"></xs:element>
</xs:sequence>
</xs:complexType>
<!-- 复合元素ComplexElement定义 -->
<xs:element name="title" type="titleType"></xs:element>
<xs:element name="body" type ="bodyType"></xs:element>
<xs:element name="window" type="windowType"></xs:element>
<xs:element name="color" type="colorType"></xs:element>
<xs:element name="menu" type="menuType"></xs:element>
<xs:element name="configuration" type="confType"></xs:element>
</xs:schema>
<color>
<red>20</red>
<green>150</green>
<blue>100</blue>
</color>