XML Schema是用一套預先規定的XML元素和屬性創建的,這些元素和屬性定義了XML文檔的結構和內容模式。XML Schema規定XML文檔實例的結構和每個元素/屬性的數據類型。

為什么要用Schema

DTD 的局限性

1.DTD不遵守XML語法(寫XML文檔實例時候用一種語法,寫DTD的時候用另外一種語法)

2.DTD數據類型有限(與數據庫數據類型不一致)

3.DTD不可擴展

4.DTD不支持命名空間(命名沖突)

.Schema的新特性

1.Schema基于XML語法

2.Schema可以用能處理XML文檔的工具處理

3.Schema大大擴充了數據類型,可以自定義數據類型

4.Schema支持元素的繼承—Object-Oriented

5.Schema支持屬性組

一:Schema基礎知識

1. Schema(模式):其作用與dtd一樣,也是用于驗證XML文檔的有效性,只不過它提供了比dtd更強大的功能和更細粒度的數據類型,另外Schema還可以自定義數據類型。此外,Schema也是一個XML文件,而dtd則不是。 

2. 所有的schema文檔,其根元素必須為schema。 

3.Schema的文檔結構

4.schema的數據類型

1.基本類型

 

2.擴展數據類型

3.數據類型的特性

二:schema的元素類型

1.schema元素:

作用:包含已經定義的schema 

用法:<xs:schema> 

?屬性

–xmlns –targetNamespace 

2.element元素作用:聲明一個元素 

屬性: –name –type –ref –minOccurs –maxOccurs 

–substitutionGroup –fixed –default 

示例:

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

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

<xs:element name="pets">


3.group元素

作用:把一組元素聲明組合在一起,以便它們能夠一起被復合類型應用 

?屬性:name/ref 

示例:

<xs:group name="myGroupOfThings"> 

<xs:sequence> 

<xs:element ref="thing1"/> 

<xs:element ref="thing2"/> 

</xs:sequence> 

</xs:group> 


4.attribute元素

作用:聲明一個屬性 

?屬性:name/type/ref/use 

?示例: 

<xs:complexType name="myComplexType"> 

<xs:attribute name="mybaseattribute" type="xs:string" use="required"/> 

</xs:complexType> 


5.attributeGroup元素

作用:把一組屬性聲明組合在一起,以便可以被復合類型應用

.屬性:name/ref

.示例:

<xs:attributeGroup name="myAttributeGroup">

<xs:attribute name="someattribute1" type="xs:integer"/>

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

</xs:attributeGroup>


6.simpleType元素

作用:定義一個簡單類型,它決定了元素和屬性值的約束和相關信息

.屬性:name

.內容:應用已經存在的簡單類型,三種方式:

restrict→限定一個范圍

list→從列表中選擇

union→包含一個值的結合

1.子元素為:<xs:restriction> 定義一個約束條件 

2.子元素為:<xs:list>從一個特定數據類型的集合中選擇定義一個簡單類型元素

3.子元素為:<xs:union>從一個特定簡單數據類型的集合中選擇定義一個簡單類型元素

示例:<xs:simpleType name="roadbikesize">

<xs:restriction base="xs:positiveInteger">

<xs:enumeration value="46"/>

<xs:enumeration value="52"/>

<xs:enumeration value="55"/>

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="mountainbikesize">

<xs:restriction base="xs:string">

<xs:enumeration value="small"/>

<xs:enumeration value="medium"/>

<xs:enumeration value="large"/>

</xs:restriction>

</xs:simpleType>

</xs:schema>


7.complexTyep類型

作用:定義一個復合類型,它決定了一組元素和屬性值的約束和相關信息 

?屬性:name 

?示例: 

<xs:complexType name="internationalShoeSize"> <xs:simpleContent> 

<xs:extension base="xs:decimal"> 

<xs:attribute name="sizing" type="xs:string"/> </xs:extension> 

</xs:simpleContent> 

</xs:complexType> 

<xs:element name="myShoeSize" type="internationalShoeSize"/>


simpleType元素和complexTyep類型的區別(重要)

simpleType類型的元素中不能包含元素或者屬性。 

?當需要聲明一個元素的子元素和/或屬性時,用complexType; 

?當需要基于內置的基本數據類型定義一個新的數據類型時,用simpleType 

8.simplecontent元素

作用:應用于complexType,對它的內容進行約束和擴展

9.choice元素

作用:允許唯一的一個元素從一個組中被選擇

.屬性:minOccurs/maxOccurs

10.sequence元素

作用:給一組元素一個特定的序列

一個完整的schema樣例:

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

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

xmlns="http://tempuri.org/po.xsd" > 

<xs:element name="purchaseOrder" type="PurchaseOrderType"/> 

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

<xs:complexType name="PurchaseOrderType"> 

<xs:sequence> 

<xs:element name="shipTo" type="USAddress"/> 

<xs:element name="billTo" type="USAddress"/> 

<xs:element ref="comment" minOccurs="0"/> 

<xs:element name="items" type="Items"/> 

</xs:sequence> 

<xs:attribute name="orderDate" type="xs:date"/> 

</xs:complexType> 

<xs:complexType name="USAddress">

<xs:sequence>

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

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

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

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

<xs:element name="zip" type="xs:decimal"/>

</xs:sequence>

<xs:attribute name="country" type="xs:NMTOKEN"

fixed="US"/>

</xs:complexType>

<xs:complexType name="Items"> 

<xs:sequence> 

<xs:element name="item" minOccurs="0" maxOccurs="unbounded"> 

<xs:complexType> 

<xs:sequence> 

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

<xs:element name="quantity"> 

<xs:simpleType> 

<xs:restriction base="xs:positiveInteger"> 

<xs:maxExclusive value="100"/> 

</xs:restriction> 

</xs:simpleType> 

</xs:element> 

<xs:element name="USPrice" type="xs:decimal"/> 

<xs:element ref="comment" minOccurs="0"/> 

<xs:element name="shipDate" type="xs:date" minOccurs="0"/> 

</xs:sequence> 

<xs:attribute name="partNum" type="SKU" use="required"/> 

</xs:complexType> 

</xs:element> 

</xs:sequence> 

</xs:complexType> 

<!-- Stock Keeping Unit, a code for identifying products --> 

<xs:simpleType name="SKU"> 

<xs:restriction base="xs:integer"> 

<xs: minInclusive=“2”/> 

<xs:maxInclusive=“10”> 

</xs:restriction> 

</xs:simpleType> 

</xs:schema> 


Schema總結:

Schema是另一種文檔類型定義,它遵循XML的語言規范。

.Schema是可擴展的,支持命名空間;

.Schema支持更多的數據類型與元素類型;

.Schemaelement聲明元素,用attribute聲明元素的屬性;

.SchemasimpleType定義簡單類型,用complexType定義復雜類型。

作者:csh624366188 發表于2012-4-6 13:03:43 原文鏈接
閱讀:272 評論:2 查看評論