XML總結(二)Schema

來源:互聯網
上載者:User

1. 最簡單的Schema文檔
如何寫一個最簡單的XML Schema文檔呢?

首先,我們寫出一個最簡單的XML文檔。

hello.xml

-------------------

<?xml version="1.0"?>

<greeting>Hello World!!</greeting>

<!--一個根項目:greeting;且這個元素不含屬性,無子項目,內容是字串。-->

hello.xsd

----------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="greeting" type="xsd:string"/>
</xsd:schema>

XML Schema文檔尾碼名是.xsd,完全符合XML文法,根項目是schema,命名空間xmlns:xsd="http://www.w3.org/2001/XMLSchema,用元素<element>定義執行個體文檔中的元素,如greeting。

2. 含子項目的Schema文檔
假設執行個體文檔是如下的:

customer.xml

-----------

<customer>

<name>teiki</name>

<address>No.237, Road Waitan, Shanghai</address>

</customer>

則可以寫出以下的XML Schema文檔:

customer.xsd

----------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="customer">

<xsd:complexType>

<xsd:sequence>

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

<xsd:element name="address" type="xsd:string" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

執行個體文檔customer.xml中,<customer>元素含有兩個子項目,所以我們在Schema文檔中採用ComplexType來定義該元素。sequence表示子項目依次出現的順序。

3. 含子項目和孫元素的Schema文檔
這次我們給出一個更加複雜一些的文檔:

customer.xml

---------------

<customer>

<name>Teiki</name>

<address>

<!-- address追加一個地址子項目 -->

<prefecture>Zhejiang</prefecture>

<city>Hangzhou</city>

<street>Xilu Road, No.121, 7F</street>

</address>

</customer>

為此,我們需要一個更加複雜一點的Schema文檔:

address.xsd

-----------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="customer">

<xsd:complexType>

<xsd:sequence>

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

<!-- 追加子項目address-->

<xsd:element name="address">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="prefecture" type="xsd:string"/>

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

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

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

不過,我們還可以採用ref元素來重新編寫這個Schema文檔:

address2.xsd

----------------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="customer">

<xsd:complexType>

<xsd:sequence>

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

<xsd:element ref="address"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="address">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="prefecture" type="xsd:string"/>

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

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

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

使用ref元素可以直接將其指向另一個模組,使文檔更加具有可讀性。 

4. 定義相同子項目的數量
先看這個簡單的訂購資料執行個體文檔:

order.xml

---------

<order>

<orderItem>Accounting Book</orderItem>

<orderItem>Taxation Book</orderItem>

</order>

假設<orderItem>元素,即每次的訂購書目不能超過10種,那該怎麼寫這個Schema文檔呢?這裡要用到<element>的maxOccurs屬性。

order.xsd

--------------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="orderItem" type="xsd:string" maxOccurs="10" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

第7行中的maxOccurs屬性為10,代表orderItem元素可以最大有10個。如果,不設定元素個數,則可以用maxOccurs="unbounded"來定義。

類似,如果要定義最小值,可以使用minOccurs,比如下面這句:

<xsd:element name="orderItem" type="xsd:string" minOccurs="5" maxOccurs="10"/>

這兩個屬性預設值都是1。

5. 定義可選項的子項目
假如上面的訂書資料中,可以用書名或者書號任一一種訂購,則執行個體文檔可能如下:

order2.xml

-----------------

<order>

<orderItem>

<!--書名訂購-->

<name>Accounting Book</name>

</orderItem>

<orderItem>

<!--書號訂購-->

<id>7-5058-3496-7</id>

</orderItem>

</order>

這時書寫Schema文檔還需要使用choice元素。

order2.xsd

-------------------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="orderItem" maxOccurs="10" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:choice>

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

<xsd:element name="id" type="xsd:string"/>

</xsd:choice>

</xsd:complexType>

</xsd:element>

</xsd:schema>

稍微更複雜的可選項子項目

再稍微修改一下訂書資料的執行個體文檔:

order3.xml

-----------------

<order>

<orderItem>

<name>Accounting Book</name>

<quantity>2</quantity>

</orderItem>

<orderItem>

<id>7-5058-3496-7</id>

</orderItem>

</order>

這裡假定<quantity>值為1時,預設。

如何修改Schema文檔呢?

order3.xsd

-----------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="orderItem" maxOccurs="10"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence>

<xsd:choice>

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

<xsd:element name="id" type="xsd:string"/>

</xsd:choice>

<xsd:element name="quantity" type="xsd:string" minOccurs="0"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

19行中的quantity最少出現值為0,也就是可以有,也可以沒有。
當然,也可以直接在<choice>元素中,包含quantity,然後定義它的minOccurs。

6. 內建簡單類型
圖省略

7. 自訂簡單類型
如果內建簡單類型的44種還不能滿足要求,怎麼辦呢?下面學習自訂簡單類型。(XML的擴充性充分體現在這裡)

例如這個執行個體文檔:

order4.xml

-----------------

<order>

<orderItem>

<id>7-5058-3496-7</id>

<quantity>5</quantity>

</orderItem>

</order>

ID是一個標準的ISBN編碼,我們怎麼定義這個ISBN編碼呢?

<xsd:simpleType name="idType">

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

<xsd:pattern value="/d{1}-/d{4}-/d{4}-/d{1}"/>

</xsd:restriction>

</xsd:simpleType>

idType是一個自訂的簡單類型。

我們對它做了限制:

<xsd:restriction base="xsd:string">代表它是基於一個字串類型。再用pattern元素來描述該字串的形式。

value="/d{1}-/d{4}-/d{4}-/d{1}"這是一個Regex,關於Regex,以後再介紹。嘻嘻!

利用這個自訂的簡單類型,我們可以重新寫Schema文檔:

order4.xsd

---------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="orderItem" maxOccurs="10"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="id" type="idType"/>

<xsd:element name="quantity" type="xsd:integer"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:simpleType name="idType">

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

<xsd:pattern value="/d{1}-/d{4}-/d{4}-/d{1}"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:schema>

假如我們事先確定好ID只有3個,即只有3個ISBN是可選的,那怎麼辦?我們可以用enumeration元素來進行列舉。

<xsd:simpleType name="idType">

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

<xsd:enumeration value="7-5058-3496-7"/>

<xsd:enumeration value="7-5005-6450-3"/>

<xsd:enumeration value="7-3020-6069-7"/>

</xsd:restriction>

</xsd:simpleType>

再來看訂購量quantity的值,如果我們設定其值必須在1-10之間,該怎麼辦呢?可以這些自訂一個簡單類型。

<xsd:simpleType name="quantityType">

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

<xsd:minInclusive value="1"/>

<xsd:maxInclusive value="10"/>

</xsd:restriction>

</xsd:simpleType>

其中,minInclusive,maxInclusive分別代表該類型的取值範圍。

所以最終修改後的Schema文檔如下:

order4-1.xsd

----------------------

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="orderItem" maxOccurs="10"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="id" type="idType"/>

<xsd:element name="quantity" type="quantityType"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:simpleType name="idType">

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

<xsd:enumeration value="7-5058-3496-7"/>

<xsd:enumeration value="7-5005-6450-3"/>

<xsd:enumeration value="7-3020-6069-7"/>

</xsd:restriction>

</xsd:simpleType>

<xsd:simpleType name="quantityType">

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

<xsd:minInclusive value="1"/>

<xsd:maxInclusive value="10"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:schema>

 

8. 定義屬性
最後,我們再來講講元素的屬性如何在Schema文檔中定義。

比如上面的order.xml執行個體文檔中:

<order>

<orderItem id="7-5058-3496-7" />

</order>

對此,我們在Schema文檔中採用一個attribute來定義:

order.xsd

---------

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence>  ←空元素

</xsd:sequence> 

<!--定義該元素屬性-->

<xsd:attribute name="id" type="xsd:string"/>

</xsd:complexType>

</xsd:element>

那麼,執行個體文檔中該屬性值是必須的還是可有可無的呢?我們可以這樣限制:

<xsd:attribute name="id" type="idType" use="required"/>

這裡我們講id屬性類型作為一種自訂資料類型idType。

而且,用attribute元素的use屬性來定義是否是必須的屬性。

required是必須值,optional是可選值,prohibited是無屬性值。

那麼對於屬性的預設值,我們怎麼定義呢?
比如:

<order>

<orderItem id="4-8443-1780-6" quantity="3"/>

</order>

我們還可以用attribute元素的另一個屬性default來定義:

<xsd:attribute name="quantity" type="xsd:integer" default="1"/>

所以,我們可以重新寫出一個Schema文檔:

order2.xsd

--------------

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence></xsd:sequence>

<xsd:attribute name="id" type="idType" use="required"/>

<xsd:attribute name="quantity" type="xsd:integer" default="1"/>

</xsd:complexType>

</xsd:element>

上面的屬性我們定義我們還可以採用屬性群組的辦法來重新改寫Schema文檔。

order3.xsd

----------------

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence></xsd:sequence>

<xsd:attributeGroup ref="orderItemAttributes"/>

</xsd:complexType>

</xsd:element>

<xsd:attributeGroup name="orderItemAttributes">

<xsd:attribute name="id" type="idType" use="required"/>

<xsd:attribute name="quantity" type="xsd:integer" default="1"/>

</xsd:attributeGroup>

這個屬性群組就不詳細解釋了,不過,大家一看就清楚了吧。

最後,我們寫一個完整的訂書order.xml的Schema文檔。

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="order">

<xsd:complexType>

<xsd:sequence>

<xsd:element ref="orderItem" maxOccurs="10"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="orderItem">

<xsd:complexType>

<xsd:sequence></xsd:sequence>

<xsd:attributeGroup ref="orderItemAttributes"/>

</xsd:complexType>

</xsd:element>

<xsd:attributeGroup name="orderItemAttributes">

<xsd:attribute name="id" type="idType" use="required"/>

<xsd:attribute name="quantity" type="xsd:integer" default="1"/>

</xsd:attributeGroup>

<xsd:simpleType name="idType">

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

<xsd:pattern value="/d{1}-/d{4}-/d{4}-/d{1}"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:schema>

其他xml中引用xsd

<?xml version="1.0" encoding="utf-8" standalone="no"?>

<Envlope xmlns="gmip" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="gmip_appraise_rep.xsd">

Xsd中對應為:

<?xml version="1.0"?>

<xsd:schema targetNamespace="gmip" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">

ElementFormDefault=”qualified” 意思是要求element使用的namespace是targetNamespace,它的作用是對元素起"限定與非限定"使用,意思是在文檔範例中要求採用命名空間首碼。

聯繫我們

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