(三)XML之Schema

来源:互联网 发布:mac制作windows安装盘 编辑:程序博客网 时间:2024/05/02 19:50

什么是Schema

XML Schema是用一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式;XML Schema规定XML文档实例的结构和每个元素/属性的数据类型。Schema(模式):其作用与dtd一样,也是用于验证XML文档的有效性,只不过它提供了比dtd更强大的功能和更细粒度的数据类型,另外Schema还可以自定义数据类型。此外,Schema也是一个XML文件,而dtd则不是。

1、同样一个XML使用DTD和Schema进行校验:

[html] view plain copy
  1. XML文档:  
  2. <书本>  
  3.     <名称>书剑恩仇录</名称>  
  4.     <作者>金庸</作者>  
  5. </书本>  
  6. DTD:  
  7. <!ELEMENT 书本 (名称,作者)>  
  8. <!ELEMENT 名称 (#PCDATA)>  
  9. <!ELEMENT 作者 (#PCDATA)>  
  10. Schema:  
  11. <element name=“书本” type=“书本类型”/>  
  12. <complexType name=“书本类型”>  
  13.     <element name=“名称” type=“string”/>  
  14.     <element name=“作者” type=“string”/>  
  15. </complexType>  


2、为什么要使用Schema

DTD 的局限性
  –DTD不遵守XML语法(写XML文档实例时候用一种语法,写DTD的时候用另外一种语法)
  –DTD数据类型有限(与数据库数据类型不一致)
  –DTD不可扩展
  –DTD不支持命名空间(命名冲突)
Schema的新特性
  –Schema基于XML语法
  –Schema可以用能处理XML文档的工具处理
  –Schema大大扩充了数据类型,可以自定义数据类型
  –Schema支持元素的继承—Object-Oriented’
  –Schema支持属性组

3、Schema的文档结构:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <<span style="color:#ff0000;">xs:schema</span> xmlns:xs="<span style="color:#ff0000;">http://www.w3.org/2001/XMLSchema</span>elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:element name="书本" type="书本类型"/>  
  4.     <xs:complexType name="书本类型">  
  5.         <xs:sequence>  
  6.             <xs:element name="名称" type="xs:string"/>  
  7.             <xs:element name="作者" type="xs:string"/>  
  8.         </xs:sequence>  
  9.     </xs:complexType>  
  10. </xs:schema>  


被验证XML文件

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <书本 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:<span style="color:#ff0000;">noNamespaceSchemaLocation</span>="file:///D:/xml/test1.xsd">  
  3.     <名称/>  
  4.     <作者/>  
  5. </书本>  


所有的schema文档,其根元素必须为schema;用于构造schema的元素和数据类型来自http://www.w3.org/2001/XMLSchema命名空间;

4、Schema的数据类型

•简单类型
  –内置的数据类型(built-in data types)
      •基本的数据类型
      •扩展的数据类型
  –用户自定义数据类型(通过simpleType定义)
      •复杂类型(通过complexType定义)

基本数据类型:

扩展数据类型:

数据类型的特性:

5、Schema的元素类型:

•schema
•element
•attribute
•group
•attributeGroup
•simpleType
•simpleContent
•complexType
choice
list
union
unique
sequence
restriction

1)schema元素
作用:包含已经定义的schema
•用法:<xs:schema>
•属性:
–xmlns
–targetNamespace

2)element元素
作用:声明一个元素
•属性:
–name
–type
–ref
–minOccurs
–maxOccurs
–substitutionGroup
–fixed
–default

举例:

[html] view plain copy
  1. <xs:element name="cat" type="xs:string"/>  
  2. <xs:element name="dog" type="xs:string"/>  
  3. <xs:element name="pets">  
  4. <xs:complexType>  
  5. <xs:sequence minOccurs="0" maxOccurs="unbounded">  
  6. <xs:element ref="cat"/>  
  7. <xs:element ref="dog"/>  
  8. </xs:sequence>  
  9. </xs:complexType>  
  10. </xs:element>  

3)group元素

•作用:把一组元素声明组合在一起,以便它们能够一起被复合类型应用
•属性:name/ref

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <<span style="color:#ff0000;">xs:group name="myGroup</span>">  
  4.         <xs:sequence>  
  5.             <xs:element name="name" type="xs:string" />  
  6.             <xs:element name="birthday" type="xs:date" />  
  7.             <xs:element name="age" type="xs:integer" />  
  8.         </xs:sequence>  
  9.     </xs:group>  
  10.     <xs:element name="person">  
  11.         <xs:complexType>  
  12.             <xs:group ref="myGroup" />  
  13.         </xs:complexType>  
  14.     </xs:element>  
  15. </xs:schema>  

4)attribute元素

•作用:声明一个属性
•属性:name/type/ref/use

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:attribute name="interest" type="xs:integer"/>  
  4.     <xs:element name="person">  
  5.         <xs:complexType>  
  6.             <xs:sequence>  
  7.                 <xs:element name="hello" type="xs:string"/>  
  8.                 <xs:element name="world" type="xs:string"/>  
  9.             </xs:sequence>  
  10.             <xs:attribute ref="interest" use="required"/>  
  11.         </xs:complexType>  
  12.     </xs:element>  
  13. </xs:schema>  


5)attributeGroup元素

•作用:把一组属性声明组合在一起,以便可以被复合类型应用
•属性:name/ref

[html] view plain copy
  1. <xs:attributeGroup name="myAttributeGroup">  
  2. <xs:attribute name="someattribute1" type="xs:integer"/>  
  3. <xs:attribute name="someattribute2" type="xs:string"/>  
  4. </xs:attributeGroup>  
  5. <xs:complexType name="myElementType">  
  6. <xs:attributeGroup ref="myAttributeGroup"/>  
  7. </xs:complexType>  

6)simpleType元素

•作用:定义一个简单类型,它决定了元素和属性值的约束和相关信息
•属性:name
•内容:应用已经存在的简单类型,三种方式:
–restrict→限定一个范围
–list→从列表中选择
–union→包含一个值的结合

《1》.子元素为:<xs:restriction>定义一个约束条件

[html] view plain copy
  1. <xs:simpleType name="myType">  
  2.         <xs:restriction base="xs:integer">  
  3.             <xs:minInclusive value="0" />  
  4.             <xs:maxInclusive value="100"/>  
  5.         </xs:restriction>  
  6. </xs:simpleType>  
  7. <xs:element name="hello" type="myType"/>  

《2》.子元素为:<xs:list>从一个特定数据类型的集合中选择定义一个简单类型元素

[html] view plain copy
  1. <xs:simpleType name="myType">  
  2.         <xs:list itemType="xs:date"/>  
  3.     </xs:simpleType>  
  4.     <xs:element name="hello" type="myType"/>  
  5.   
  6.   
  7. <?xml version="1.0" encoding="UTF-8"?>  
  8. <hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test7.xsd" >  
  9. 2012-01-01 2012-01-01  
  10. 2012-01-01 2012-01-01  
  11. </hello>  


《3》.子元素为:<xs:union>从一个特定简单数据类型的集合中选择定义一个简单类型元素

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:attribute name="allFrameSize">  
  4.         <xs:simpleType>  
  5.             <xs:<span style="color:#ff0000;">union</span> <span style="color:#ff0000;">memberTypes</span>="<span style="color:#3333ff;">roadbikeSize</span> <span style="color:#3333ff;">mountainbikeSize</span>/>  
  6.         </xs:simpleType>  
  7.     </xs:attribute>  
  8.       
  9.     <xs:simpleType name="<span style="color:#3333ff;">roadbikeSize</span>">  
  10.         <xs:restriction base="xs:positiveInteger">  
  11.             <xs:enumeration value="46"/>  
  12.             <xs:enumeration value="55"/>  
  13.             <xs:enumeration value="70"/>  
  14.         </xs:restriction>   
  15.     </xs:simpleType>  
  16.       
  17.     <xs:simpleType name="<span style="color:#3333ff;">mountainbikeSize</span>">  
  18.         <xs:restriction base="xs:string">  
  19.             <xs:enumeration value="small"/>  
  20.             <xs:enumeration value="medium"/>  
  21.             <xs:enumeration value="large"/>  
  22.         </xs:restriction>  
  23.     </xs:simpleType>  
  24.     <xs:element name="hello">  
  25.         <xs:complexType>  
  26.             <xs:sequence>  
  27.                 <xs:element name="welcome" type="xs:string"/>  
  28.             </xs:sequence>  
  29.             <xs:attribute ref="allFrameSize" use="required"/>  
  30.         </xs:complexType>  
  31.     </xs:element>  
  32. </xs:schema>  

7)complexType元素

•作用:定义一个复合类型,它决定了一组元素和属性值的约束和相关信息
•属性:name

8)complexType与simpleType的区别

•simpleType类型的元素中不能包含元素或者属性。
•当需要声明一个元素的子元素和/或属性时,用complexType;
•当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType。
1) SimpleType类型的元素没有子元素,也没有属性。
2) 当需要定义的元素包含了子元素或者属性时,必须要使用ComplexType。

9)simpleContent元素

作用:应用于complexType,对它的内容进行约束和扩展,元素的内容和属性都定义在simpleContent中,同时,元素下不包括子元素,所以必定包含属性,(extension表示这个元素类型)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:element name="shoeSize">  
  4.         <xs:complexType>  
  5.             <xs:simpleContent>  
  6.                 <xs:<span style="color:#ff0000;">extension</span> base="xs:decimal">  
  7.                 <xs:attribute name="sizing" use="required">  
  8.                     <xs:simpleType>  
  9.                         <xs:restriction base="xs:string">  
  10.                             <xs:enumeration value="us"/>  
  11.                             <xs:enumeration value="uk"/>  
  12.                             <xs:enumeration value="un"/>  
  13.                         </xs:restriction>  
  14.                     </xs:simpleType>  
  15.                 </xs:attribute>  
  16.                 </xs:extension>  
  17.             </xs:simpleContent>  
  18.         </xs:complexType>  
  19.     </xs:element>  
  20. </xs:schema>  


SimpleContent,用于ComplexType元素上,用于限定该ComplexType的内容类型,表示该ComplexType没有子元素,同时该ComplexType需要有属性,否则它就成为SimpleType了。

10)choice元素

•作用:允许唯一的一个元素从一个组中被选择
•属性:minOccurs/maxOccurs

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:complexType name="myType">  
  4.         <xs:choice minOccurs="1" maxOccurs="3">  
  5.             <xs:element name="hello" type="xs:string" />  
  6.             <xs:element name="world" type="xs:string"/>  
  7.         </xs:choice>  
  8.     </xs:complexType>  
  9.       
  10.     <xs:element name="helloworld" type="myType" />  
  11. </xs:schema>  

choice的次数minOccurs="1" maxOccurs="3"是指下面的整体出现次数。

11)sequence元素

•作用:给一组元素一个特定的序列

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">  
  3.     <xs:complexType name="myType">  
  4.         <xs:<span style="color:#ff0000;">sequence</span> minOccurs="1" maxOccurs="3">  
  5.             <xs:element name="hello" type="xs:string" />  
  6.             <xs:element name="world" type="xs:string"/>  
  7.         </xs:sequence>  
  8.     </xs:complexType>  
  9.       
  10.     <xs:element name="helloworld" type="myType" />  
  11. </xs:schema>  


6、通过DOCTYPE可以明确指定文档的根元素,因为DOCTYPE后面跟的元素就是文档的根元素;通过Schema是没法明确指定目标XML文档的根元素,XmlSpy是通过推断哪个元素包含了其他元素来选择包含其他元素最多的那个元素作为文档的根,但我们可以明确指定文档的根元素而不必按照XmlSpy的生成来做。

•Schema是另一种文档类型定义,它遵循XML的语言规范。
•Schema是可扩展的,支持命名空间;
•Schema支持更多的数据类型与元素类型;
•Schema用element声明元素,用attribute声明元素的属性;
•Schema用simpleType定义简单类型,用complexType定义复杂类型。

0 0