一個 XSD 執行個體

來源:互聯網
上載者:User

http://www.w3school.com.cn/schema/schema_example.asp

本節會為您示範如何編寫一個 XML Schema。您還將學習到編寫 schema 的不同方法。

XML 文檔

讓我們看看這個名為 "shiporder.xml" 的 XML 文檔:

<?xml version="1.0" encoding="ISO-8859-1"?><shiporder orderid="889923"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>George Bush</orderperson> <shipto>  <name>John Adams</name>  <address>Oxford Street</address>  <city>London</city>  <country>UK</country> </shipto> <item>  <title>Empire Burlesque</title>  <note>Special Edition</note>  <quantity>1</quantity>  <price>10.90</price> </item> <item>  <title>Hide your heart</title>  <quantity>1</quantity>  <price>9.90</price> </item></shiporder>

上面的XML文檔包括根項目 "shiporder",其中包含必須名為 "orderid" 的屬性。"shiporder" 元素包含三個不同的子項目:"orderperson"、"shipto" 以及 "item"。"item" 元素出現了兩次,它含有一個 "title"、一個可選 "note" 元素、一個 "quantity" 以及一個 "price" 元素。

上面這一行 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",告知XML解析器根據某個 schema 來驗證此文檔。這一行:xsi:noNamespaceSchemaLocation="shiporder.xsd" 規定了 schema 的位置(在這裡,它與 "shiporder.xml" 處於相同的檔案夾)。

建立一個 XML Schema

現在,我們需要為上面這個 XML 文檔建立一個 schema。

我們可以通過開啟一個新的檔案來開始,並把這個檔案命名為 "shiporder.xsd"。要建立schema,我們僅僅需要簡單地遵循 XML 文檔中的結構,定義我們所發現的每個元素。首先我們開始定義一個標準的 XML 聲明:

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">......</xs:schema>

在上面的 schema 中,我們使用了標準的命名空間 (xs),與此命名空間相關聯的 URI 是 Schema 的語言定義(Schema language definition),其標準值是 http://www.w3.org/2001/XMLSchema。

接下來,我們需要定義 "shiporder" 元素。此元素擁有一個屬性,其中包含其他的元素,因此我們將它認定為複合類型。"shiporder" 元素的子項目被 xs:sequence 元素包圍,定義了子項目的次序:

<xs:element name="shiporder"> <xs:complexType>  <xs:sequence>  ...  ...  </xs:sequence>  ... </xs:complexType></xs:element>

然後我們需要把 "orderperson" 元素定義為簡易類型(這是因為它不包含任何屬性或者其他的元素)。類型 (xs:string) 的首碼是由命名空間的首碼規定的,此命名空間與指示預定義的 schema 資料類型的 XML schema 相關聯:

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

接下來,我需要把兩個元素定義為複合類型:"shipto" 和 "item"。我們從定義 "shipto" 元素開始:

<xs:element name="shipto"> <xs:complexType>  <xs:sequence>   <xs:element name="name" type="xs:string"/>   <xs:element name="address" type="xs:string"/>   <xs:element name="city" type="xs:string"/>   <xs:element name="country" type="xs:string"/>  </xs:sequence> </xs:complexType></xs:element>

通過 schema,我們可使用 maxOccurs 和 minOccurs 屬性來定義某個元素可能出現的次數。maxOccurs 定義某元素出現次數的最大值,而 minOccurs 則定義某元素出現次數的最小值。maxOccurs 和 minOccurs 的預設值都是 1!

現在,我們可以定義 "item" 元素了。這個元素可在 "shiporder" 元素內部出現多次。這是通過把 "item" 元素的 maxOccurs 屬性的值設定為 "unbounded" 來實現的,這樣 "item" 元素就可出現創作者所希望的任意多次。請注意,"note" 元素是可選元素。我們已經把此元素的 minOccurs 屬性設定為 0 了:

<xs:element name="item" maxOccurs="unbounded"> <xs:complexType>  <xs:sequence>   <xs:element name="title" type="xs:string"/>   <xs:element name="note" type="xs:string" minOccurs="0"/>   <xs:element name="quantity" type="xs:positiveInteger"/>   <xs:element name="price" type="xs:decimal"/>  </xs:sequence> </xs:complexType></xs:element>

現在,我們可以聲明 "shiporder" 元素的屬性了。由於這是一個必選屬性,我們規定 use="required"。

注釋:此屬性的聲明必須被置於最後:

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

這是這個名為 "shiporder.xsd" 的 schema 檔案的文檔清單:

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="shiporder"> <xs:complexType>  <xs:sequence>   <xs:element name="orderperson" type="xs:string"/>   <xs:element name="shipto">    <xs:complexType>     <xs:sequence>      <xs:element name="name" type="xs:string"/>      <xs:element name="address" type="xs:string"/>      <xs:element name="city" type="xs:string"/>      <xs:element name="country" type="xs:string"/>     </xs:sequence>    </xs:complexType>   </xs:element>   <xs:element name="item" maxOccurs="unbounded">    <xs:complexType>     <xs:sequence>      <xs:element name="title" type="xs:string"/>      <xs:element name="note" type="xs:string" minOccurs="0"/>      <xs:element name="quantity" type="xs:positiveInteger"/>      <xs:element name="price" type="xs:decimal"/>     </xs:sequence>    </xs:complexType>   </xs:element>  </xs:sequence>  <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType></xs:element></xs:schema>
分割 Schema

前面的設計方法非常容易,但當文檔很複雜時卻難以閱讀和維護。

接下來介紹的設計方法基於首先對所有元素和屬性的定義,然後再使用 ref 屬性來引用它們。

這是用新方法設計的 schema 檔案:

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><!-- 簡易元素的定義 --><xs:element name="orderperson" type="xs:string"/><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/><!-- 屬性的定義 --><xs:attribute name="orderid" type="xs:string"/><!-- 複合元素的定義 --><xs:element name="shipto"> <xs:complexType>  <xs:sequence>   <xs:element ref="name"/>   <xs:element ref="address"/>   <xs:element ref="city"/>   <xs:element ref="country"/>  </xs:sequence> </xs:complexType></xs:element><xs:element name="item"> <xs:complexType>  <xs:sequence>   <xs:element ref="title"/>   <xs:element ref="note" minOccurs="0"/>   <xs:element ref="quantity"/>   <xs:element ref="price"/>  </xs:sequence> </xs:complexType></xs:element><xs:element name="shiporder"> <xs:complexType>  <xs:sequence>   <xs:element ref="orderperson"/>   <xs:element ref="shipto"/>   <xs:element ref="item" maxOccurs="unbounded"/>  </xs:sequence>  <xs:attribute ref="orderid" use="required"/> </xs:complexType></xs:element></xs:schema>
使用指定的類型(Named Types)

第三種設計方法定義了類或者類型,這樣使我們有能力重複使用元素的定義。具體的方式是:首先對簡易元素和複合元素進行命名,然後通過元素的 type 屬性來指向它們。

這是利用第三種方法設計的 schema 檔案 ("shiporder.xsd"):

<?xml version="1.0" encoding="ISO-8859-1" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/></xs:simpleType><xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/></xs:simpleType><xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/></xs:simpleType><xs:simpleType name="orderidtype"> <xs:restriction base="xs:string">  <xs:pattern value="[0-9]{6}"/> </xs:restriction></xs:simpleType><xs:complexType name="shiptotype"> <xs:sequence>  <xs:element name="name" type="stringtype"/>  <xs:element name="address" type="stringtype"/>  <xs:element name="city" type="stringtype"/>  <xs:element name="country" type="stringtype"/> </xs:sequence></xs:complexType><xs:complexType name="itemtype"> <xs:sequence>  <xs:element name="title" type="stringtype"/>  <xs:element name="note" type="stringtype" minOccurs="0"/>  <xs:element name="quantity" type="inttype"/>  <xs:element name="price" type="dectype"/> </xs:sequence></xs:complexType><xs:complexType name="shipordertype"> <xs:sequence>  <xs:element name="orderperson" type="stringtype"/>  <xs:element name="shipto" type="shiptotype"/>  <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/></xs:complexType><xs:element name="shiporder" type="shipordertype"/></xs:schema>

restriction 元素顯示出資料類型源自於 W3C XML Schema 命名空間的資料類型。因此,下面的片段也就意味著元素或屬性的值必須是字串類型的值:

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

restriction 元素常被用於向元素施加限制。請看下面這些來自以上 schema 的片段:

<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string">  <xs:pattern value="[0-9]{6}"/> </xs:restriction></xs:simpleType>

這段代碼指示出,元素或屬性的值必須為字串,並且必須是連續的六個字元,同時這些字元必須是 0-9 的數字。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.