(三)XML之Schema

什麼是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定義複雜類型。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章