Java程式員從笨鳥到菜鳥之(二十五)XML之Schema驗證

來源:互聯網
上載者:User

         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; 

•當需要基於內建的基礎資料型別 (Elementary Data Type)定義一個新的資料類型時,用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支援更多的資料類型與元素類型;

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

.Schema用simpleType定義簡單類型,用complexType定義複雜類型。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.