[b]简介[/b]
XML Schema 是 W3C 标准。
XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 描述 XML 文档的结构。
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。
详细内容请参考[url]http://www.w3.org/XML/Schema[/url]

XML Schema 是符合 XML 语法的文档,是形式良好的 XML 文档,比如:
[list]
[*]它必须以 XML 声明开头
[*]它必须拥有唯一的根元素
[*]开始标签必须与结束标签相匹配
[*]元素对大小写敏感
[*]所有的元素都必须关闭
[*]所有的元素都必须正确地嵌套
[*]必须对特殊字符使用实体
[/list]

一个简单的例子:

<?xml version="1.0" encoding="GBK"?>
<circle xmlns:an="http://www.an.net/shape" name="demoCircle">
  <center>
    <x>155</x>
    <y>231</y>
  </center>
  <radius>15.59</radius>
</circle>



<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:an="http://www.an.net/shape" 
targetNamespace="http://www.an.net/shape" elementFormDefault="qualified">
  <xs:element name="circle">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="center" type="an:PointType"/>
        <xs:element name="radius" type="xs:double"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="PointType">
    <xs:sequence>
      <xs:element name="x" type="xs:nonPositiveInteger"/>
      <xs:element name="y" type="xs:nonPositiveInteger"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>




[b]简易元素[/b]


简易元素指那些仅包含文本的元素。它不会包含任何其他的元素或属性。


不过,“仅包含文本”这个限定却很容易造成误解。文本有很多类型。它可以是 XML Schema 定义中包括的类型中的一种(布尔、字符串、数据等等),或者它也可以是您自行定义的定制类型。


定义简易元素的语法:


<xs:element name="xxx" type="yyy"/>



此处 xxx 指元素的名称,yyy 指元素的数据类型。XML Schema 拥有很多内建的数据类型。


例如 radius 就是一个简易元素。


定义属性的语法是:


<xs:attribute name="xxx" type="yyy"/>



在此处,xxx 指属性名称,yyy 则规定属性的数据类型。XML Schema 拥有很多内建的数据类型。


简易元素无法拥有属性。假如某个元素拥有属性,它就会被当作某种复合类型。所有的属性均作为简易类型来声明。



最常用的类型是:


[list]


[*]xs:string


[*]xs:decimal


[*]xs:integer


[*]xs:boolean


[*]xs:date


[*]xs:time


[/list]


[b]复合元素[/b]


复合元素指包含其他元素及/或属性的 XML 元素。


有四种类型的复合元素:


[list]


[*]空元素


[*]包含其他元素的元素


[*]仅包含文本的元素


[*]包含元素和文本的元素


[/list]


注释:上述元素均可包含属性!


例如 PointType 就是一个复合元素。



[b]复合空元素示例:[/b]


<product prodid="1345"/>



xsd定义1


<xs:element name="product">
  <xs:complexType>
    <xs:attribute name="prodid" type="xs:positiveInteger"/>
  </xs:complexType>
</xs:element>



xsd定义2


<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">
  <xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>



[b]复合类型仅包含元素:[/b]


<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>



xsd定义1


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



xsd定义2


<xs:element name="person" type="persontype"/>

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



[b]仅含文本的复合元素:[/b]


<shoesize country="france">35</shoesize>



xsd定义1


<xs:element name="shoesize">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="country" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>



xsd定义2


<xs:element name="shoesize" type="shoetype"/>

<xs:complexType name="shoetype">
  <xs:simpleContent>
    <xs:extension base="xs:integer">
      <xs:attribute name="country" type="xs:string" />
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>



对于仅包含文本的请使用 extension 或 restriction 元素来扩展或限制元素的基本简易类型:


<xs:extension base="basetype">
  ....
  ....
</xs:extension>
<xs:restriction base="basetype">
  ....
  ....
</xs:restriction>



[b]带有混合内容的复合类型:[/b]


<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>



xsd定义1


<xs:element name="letter">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="orderid" type="xs:positiveInteger"/>
      <xs:element name="shipdate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>



xsd定义2


<xs:element name="letter" type="lettertype"/>

<xs:complexType name="lettertype" mixed="true">
  <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="orderid" type="xs:positiveInteger"/>
    <xs:element name="shipdate" type="xs:date"/>
  </xs:sequence>
</xs:complexType>





[b]XSD 复合类型指示器[/b]


通过指示器,我们可以控制在文档中使用元素的方式。



[b]Order 指示器[/b]


Order 指示器用于定义元素的顺序。


[list]


[*]All


[*]Choice


[*]Sequence


[/list]


[u]<all>[/u] 指示器规定子元素可以按照任意顺序出现,且每个子元素必须只出现一次:


<xs:element name="person">
  <xs:complexType>
    <xs:all>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>



[u]<choice> [/u]指示器规定可出现某个子元素或者可出现另外一个子元素(非此即彼):


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



[u]<sequence>[/u] 规定子元素必须按照特定的顺序出现:


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



[b]Occurrence 指示器[/b]


Occurrence 指示器用于定义某个元素出现的频率。对于所有的 "Order" 和 "Group" 指示器(any、all、choice、sequence、group name 以及 group reference),其中的 maxOccurs 以及 minOccurs 的默认值均为 1。


[list]


[*]maxOccurs


[*]minOccurs


[/list]


[u]<maxOccurs> [/u]指示器可规定某个元素可出现的最大次数:


<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>

</xs:element>



[u]<minOccurs> [/u]指示器可规定某个元素能够出现的最小次数:


<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string"
      maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>



如需使某个元素的出现次数不受限制,请使用 maxOccurs="unbounded" 这个声明。


[b]Group 指示器[/b]


Group 指示器用于定义相关的数批元素。


[list]


[*]Group name


[*]attributeGroup name


[/list]


[b]元素组[/b]


元素组通过 group 声明进行定义:


<xs:group name="组名称">


...


</xs:group>


您必须在 group 声明内部定义一个 all、choice 或者 sequence 元素。下面这个例子定义了名为 "persongroup" 的 group,它定义了必须按照精确的顺序出现的一组元素:


<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>



在您把 group 定义完毕以后,就可以在另一个定义中引用它了:


<xs:group name="persongroup">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="birthday" type="xs:date"/>
  </xs:sequence>
</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:group ref="persongroup"/>
    <xs:element name="country" type="xs:string"/>
  </xs:sequence>
</xs:complexType>



[b]属性组[/b]



属性组通过 attributeGroup 声明来进行定义:



<xs:attributeGroup name="组名称">


...


</xs:attributeGroup>



下面这个例子定义了名为 "personattrgroup" 的一个属性组:


<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>



在您已定义完毕属性组之后,就可以在另一个定义中引用它了,就像这样:


<xs:attributeGroup name="personattrgroup">
  <xs:attribute name="firstname" type="xs:string"/>
  <xs:attribute name="lastname" type="xs:string"/>
  <xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>

<xs:element name="person">
  <xs:complexType>
    <xs:attributeGroup ref="personattrgroup"/>
  </xs:complexType>
</xs:element>