1 / 24

Introduction to XML Schema

Introduction to XML Schema. XML Schema. XML Schema 是針對 XML 所發展的新一代驗證機制,其主要的目的是在取代 DTD 。 XML Schema 或稱為 XSD(XML Schema Definition) 原本是微軟提出的規格,在 2001 年 5 月成為 W3C 的建議規格。 XML Schema 不像 DTD 擁有自己的定義語法,它本身就是一份良好格式的 XML 文件。 XML Schema 支援更多 XML 元素的「資料型態」 (Data types) ,可以定義各種 XML 元素的資料型態。

gerodi
Télécharger la présentation

Introduction to XML Schema

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to XML Schema

  2. XML Schema • XML Schema是針對XML所發展的新一代驗證機制,其主要的目的是在取代DTD。 • XML Schema或稱為XSD(XML Schema Definition)原本是微軟提出的規格,在2001年5月成為W3C的建議規格。 • XML Schema不像DTD擁有自己的定義語法,它本身就是一份良好格式的XML文件。 • XML Schema支援更多XML元素的「資料型態」(Data types),可以定義各種XML元素的資料型態。 • XML Schema支援名稱空間,但是DTD不支援,所以XML Schema比DTD擁有更大的擴充性。

  3. XML Schema • XML Schema擁有擴充性:XML Schema是一份XML文件,可以重複使用其它的XML Schema、建立自已的資料型態和同時使用多份XML Schema文件驗證XML文件。 • XML Schema使用XML語法 • XML Schema支援資料型態:可以如同資料庫一般,詳細描述文件內容各元素的資料,並且在不同的元素間輕鬆的轉換資料。 • XML Schema建立保密的資料通訊:XML Schema可以詳細描述傳送XML文件的元素資料,只讓接收者知道是什麼資料

  4. XML Schema的基本架構 • XML Schema文件的基本架構是一份XML文件,其根元素為schema,如下所示:<?xml version="1.0" encoding="Big5"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema” elementFormDefault="qualified“> ……..</xs:schema> • 上述文件架構使用XML Schema基礎名稱空間的字頭xs,其預設使用的的W3C的名稱空間

  5. XML Schema的基本架構 • 在元素schema間的子元素是定義Instance文件的XML元素,常用的Schema元素,如下表所示:

  6. 使用XML Schema • 通常將XML Schema獨立儲存成Schema文件檔案,副檔名為.xsd,在XML的Instance文件指定使用的Schema檔案。<root_node xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance“ xsi:noNamespaceSchemaLocation="Ch5-2.xsd"> • 在XML文件的根元素指定XML文件使用的XML Schema,xsi為Instance的XML文件預設使用的名稱空間,這也是W3C的名稱空間,如下所示:http://www.w3.org/2001/XMLSchema-instance

  7. 使用XML Schema 01: <?xml version="1.0" encoding="Big5"?> 02: <!--網頁製作徹底研究系列--> 03: <booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 04: xsi:noNamespaceSchemaLocation="Ch5-2.xsd"> 05: <book sales="Y"> 06: <code>F8915</code> 07: <title>ASP網頁製作徹底研究</title> 08: <authorlist> 09: <author>陳會安</author> 10: </authorlist> 11: <price>580</price> 12: </book> 13: <book sales="N"> 14: <code>F8916</code> 15: <title>ASP與IIS4/5網站架設徹底研究</title> 16: <authorlist> 17: <author>陳會安</author> 18: </authorlist> 19: <price>550</price> 20: </book> 21: </booklist>

  8. 使用XML Schema 01: <?xml version="1.0" encoding="Big5"?> 02: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 03: elementFormDefault="qualified"> 04: <xs:element name="booklist"> 05: <xs:complexType> 06: <xs:sequence> 07: <xs:element name="book" type="bookType" maxOccurs="unbounded"/> 08: </xs:sequence> 09: </xs:complexType> 10: </xs:element> 11: <xs:complexType name="bookType"> 12: <xs:sequence> 13: <xs:element name="code" type="xs:string"/> 14: <xs:element name="title" type="xs:string"/> 15: <xs:element name="authorlist" type="authorlistType"/> 16: <xs:element name="price" type="xs:decimal"/> 17: </xs:sequence>

  9. 使用XML Schema 18: <xs:attribute name="sales" use="required"> 19: <xs:simpleType> 20: <xs:restriction base="xs:NMTOKEN"> 21: <xs:enumeration value="N"/> 22: <xs:enumeration value="Y"/> 23: </xs:restriction> 24: </xs:simpleType> 25: </xs:attribute> 26: </xs:complexType> 27: <xs:complexType name="authorlistType"> 28: <xs:sequence minOccurs="1" maxOccurs="5"> 29: <xs:element name="author" type="xs:string"/> 30: </xs:sequence> 31: </xs:complexType> 32: </xs:schema>

  10. XML Schema內建的資料型態 • XML Schema支援內建的資料型態,能夠直接定義XML元素或屬性內容 • <xs:element name="price" type="xs:decimal"/> • <xs:attribute name="lang" type="xs:string" default="Chinese"/> • 上述element和attribute標籤的type屬性使用的屬於內建的資料型態decimal和string,可以定義XML元素price和屬性lang內容的資料型態。

  11. 自訂資料型態simpleType元素 • 若XML Schema內建的資料型態仍不符合使用者的需求時,可使用simpleType元素自訂所需的資料型態 • 所使用的內建資料型態稱為「基底」(Base)資料型態,由從這個資料型態衍生出自訂的資料型態。 • 使用restriction子元素宣告使用的基底資料型態 • <xs:simpleType name="name“> <xs:restriction base="xs:datatypes“> <xs:facets_element value= "value"/> …… </xs:restriction></xs:simpleType>

  12. 自訂資料型態simpleType元素 • restriction元素的子元素facets_element是描述資料型態的細節限制,例如:長度、範圍和列舉清單等

  13. 自訂資料型態simpleType元素 • 定義數字的範圍 -使用simpleType元素定義一個範圍的數字資料型態 • 定義列舉的元素內容 -元素內容為列舉清單的其中之一 <xs:simpleType name="orderQuantity“> <xs:restriction base="xs:integer“> <xs:minInclusive value="1"/> <xs:maxInclusive value="100"/> </xs:restriction></xs:simpleType> <xs:simpleType name="colorType"> <xs:restriction base="xs:string"> <xs:enumeration value="red"/> <xs:enumeration value="white"/> <xs:enumeration value="blue"/> <xs:enumeration value="black"/> </xs:restriction> </xs:simpleType>

  14. 自訂資料型態simpleType元素 • 字串長度與正規語言(Regular Expressions) • 元素值為字串,可以使用length、minLength和maxLength元素定義字串的長度 • 可以使用pattern定義字串比對的正規語言的範本字串 <xs:simpleType name="productCode"> <xs:restriction base="xs:string"> <xs:length value="6" fixed="true"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="phoneNumber"> <xs:restriction base="xs:string"> <xs:pattern value="\d{2}-\d{8}"/> </xs:restriction> </xs:simpleType>

  15. XML元素宣告 • name屬性:此為XML元素名稱。 • type屬性:XML元素內容的資料型態,可以使用內建資料型態、simpleType和complexType元素宣告的資料型態。 • minOccurs屬性:元素最少出現的次數。 • maxOccurs屬性:元素最大出現的次數,值unbounded表示無限次數。 <element name="name" type="type" minOccurs="int" maxOccurs="int"/>

  16. XML文件架構的complexType元素 • complexType元素可以宣告子元素或屬性的名稱和資料型態 • name屬性:此為資料型態的名稱。 • mixed屬性:說明此元素的內容為何,預設值false表示只宣告XML元素,值true表示宣告XML元素和文字內容 <xs:complexType name="name" mixed="true"> <xs:sequence> <xs:element …/> <xs:element …/> </xs:sequence> </xs:complexType>

  17. XML文件架構的complexType元素 • all任易順序排列元素:all元素宣告XML子元素可以使用任易順序出現 • sequence順序元素:sequence元素可以宣告XML子元素出現的順序 <xs:complexType name="bookType"> <xs:all> <xs:element ref="code"/> <xs:element ref="title"/> <xs:element ref="authorlist"/> <xs:element ref="price"/> </xs:all> </xs:complexType> <xs:complexType name="bookType"> <xs:sequence> <xs:element ref="code" minOccurs="1" maxOccurs="1"/> <xs:element ref="title" minOccurs="1" maxOccurs="1"/> <xs:element ref="authorlist" minOccurs="1" maxOccurs="1"/> <xs:element ref="price" minOccurs="1" maxOccurs="1"/> </xs:sequence> </xs:complexType>

  18. XML文件架構的complexType元素 • choice選項元素:指定為選項元素的XML元素 • group群組元素:將基本的XML元素結合成為一個group元素 <xs:choice> <xs:element name="a" type="string" minOccurs="1" maxOccurs="1"/> <xs:element name="b" type="string" minOccurs="1" maxOccurs="1"/> <xs:element name="c" type="string" minOccurs="1" maxOccurs="1"/> </xs:choice> <xs:group name="bookData"> <xs:sequence> <xs:element name="code" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="authorlist" type="authorlistType"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:group>

  19. 宣告XML元素的屬性 • 在宣告complexType元素或element元素時,同時宣告元素的屬性,使用的是attribute元素 <attribute name="name" type="simple_type" use="how_its_used" default="value" fixed="value"/> <xs:complexType name="bookType"> <xs:sequence> ……. </xs:sequence> <xs:attribute name="instock" use="required" type="xs:boolean"/> </xs:complexType>

  20. attributeGroup元素 • XML屬性一樣也可以使用attributeGroup元素建立群組屬性 • 可以在宣告XML元素book時,直接套用群組屬性 <xs:attributeGroup name="bookAttr"> <xs:attribute name="code" type="xs:ID" use="required"/> <xs:attribute name="title" type="xs:string" use="required"/> <xs:attribute name="author" type="xs:string" use="required"/> <xs:attribute name="price" type="xs:decimal" use="required"/> </xs:attributeGroup> <xs:element name="book" minOccurs="1" maxOccurs="unbounded"> <xs:complexType> <xs:attributeGroup ref="bookAttr"/> </xs:complexType> </xs:element>

  21. 個人履歷表的XML Schema 01: <?xml version="1.0" encoding="Big5"?> 02: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 03: <xs:element name="resume"> 04: <xs:complexType> 05: <xs:sequence> 06: <xs:element name="gereralinfo" type="gereralinfoType"/> 07: <xs:element name="workinginfo" type="workinginfoType"/> 08: </xs:sequence> 09: </xs:complexType> 10: </xs:element> 11: <xs:complexType name="gereralinfoType"> 12: <xs:sequence> 13: <xs:element ref="name"/> 14: <xs:element ref="birthday"/> 15: <xs:element ref="address"/> 16: <xs:element name="phonelist" type="phonelistType"/> 17: </xs:sequence> 18: </xs:complexType>

  22. 個人履歷表的XML Schema 19: <xs:element name="name" type="xs:string"/> 20: <xs:element name="address" type="xs:string"/> 21: <xs:element name="birthday" type="xs:string"/> 22: <xs:complexType name="phonelistType"> 23: <xs:sequence> 24: <xs:element ref="homephonenumber"/> 25: <xs:element ref="companyphonenumber"/> 26: <xs:element ref="cellphonenumber"/> 27: </xs:sequence> 28: </xs:complexType> 29: <xs:element name="cellphonenumber" type="xs:string"/> 30: <xs:element name="companyphonenumber" type="xs:string"/> 31: <xs:element name="homephonenumber" type="xs:string"/> 32: <xs:complexType name="workinginfoType">

  23. 個人履歷表的XML Schema 33: <xs:sequence> 34: <xs:element name="job" type="jobType" maxOccurs="unbounded"/> 35: </xs:sequence> 36: </xs:complexType> 37: <xs:complexType name="jobType"> 38: <xs:sequence> 39: <xs:element ref="companyname"/> 40: <xs:element ref="title"/> 41: <xs:element ref="description"/> 42: </xs:sequence> 43: <xs:attribute name="code" type="xs:ID" use="required"/> 44: </xs:complexType> 45: <xs:element name="title" type="xs:string"/> 46: <xs:element name="companyname" type="xs:string"/> 47: <xs:element name="description" type="xs:string"/> 48: </xs:schema>

  24. XML Schema基礎名稱空間 • 在XML Schema建議規格擁有兩個基礎名稱空間,第一個是用在Schema文件的Schema URI,如下所示: • http://www.w3.org/2001/XMLSchema • 上述名稱空間是用在Schema文件,通常我們使用名稱空間字頭xs,至於Instance的XML文件則是使用Instance URI,如下所示: • http://www.w3.org/2001/XMLSchema-instance • 上述名稱空間是用在Instance的XML文件,通常使用名稱空間字頭xsi。

More Related