XML Schema學習筆記和註解-(轉)

1、複雜型和簡單型之間最根本的區別就是:複雜型的內容中可以包含其他元素,也可以帶有屬性(Attribute),但簡單型既不能包含子元素,也不能帶有任何屬性,但限制條件或擴展條件還是可以有的。

一個複雜類型例子:
<xsd:complexType >
    <xsd:sequence>
        <xsd:element name="name"type="xsd:string"/>
        <xsd:element type="xsd:string"/>
        <xsd:element type="xsd:string"/>
        <xsd:element type="xsd:decimal"/>
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>

一個簡單類型例子:
<xsd:simpleType >
    <xsd:restriction base="xsd:integer">
        <xsd:minInclusive value="10000"/>
        <xsd:maxInclusive value="99999"/>
    </xsd:restriction>
</xsd:simpleType> 


2、element存在約束:element可以通過其minOccurs和maxOccurs兩個屬性來約束元素實例存在的個數,這兩個屬性的缺省值都是1,表示默認情況下此元素在XML實例文檔中必須出現一次。
例如:
<element minOccurs="1" maxOccurs="1" type="timeInstant"/>

3、attribute存在約束:元素屬性也可以通過attribute的use屬性來約束出現一次或根本不出現;use屬性的取值可以是required,optional,prohibited三個值,缺省(默認)值是optional. 


4、 element和attribute都有一個default和fixed屬性,不能同時存在。針 對element來說,只有當element實例爲空時才採用此 default值,而attribute是當實例不提供此attribute時才採用此default值,因此對attribute而言,只有其use值 是optional時default值纔有意義,而且對element和attribute來說fixed和default兩個屬性不能同時存在,否則會 出現錯誤。 
fixed屬性要求element或attribute如果出現的話,就一定要是fixed屬性裏指定的值。


5、全局元素和全局屬性。直接定義在schema元素下,即schema元素的頂級子元素的element和attribute都是全局的,稱之爲全局元素和全局屬性,你在其他型定義中可以直接引用。 

從上面這個例子可以看出,purchaseOrder和comment是全局元素,可以被其他element用ref屬性引用,purchaseOrderType,USAddress,Items和SKU是全局屬性,可以被其他element用type屬性引用。

6、派生新型有兩種方式:第一種就是直接從其他型中擴展(繼承)而來,另外一種就是通過對已有型進行限定性約束而來。 
如:以下有三種通過限定性約束定義的新型: 
通過值範圍限定: 
<xsd:simpleType >
    <xsd:restriction base="xsd:integer">
        <xsd:minInclusive value="10000"/>
        <xsd:maxInclusive value="99999"/>
    </xsd:restriction>
</xsd:simpleType> 
使用模式匹配限定: 
<xsd:simpleType >
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="/d{3}-[A-Z]{2}"/>
    </xsd:restriction>
</xsd:simpleType> 
使用枚舉方式限定: 
<xsd:simpleType >
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="BeiJing"/>
        <xsd:enumeration value="NanChang"/>
        <xsd:enumeration value="ShangHai"/>
    </xsd:restriction>
</xsd:simpleType> 

7、原子型(不可分割的型,象string,integer等系統內建的型)、列表型、聯合型合起來統一稱爲簡單型。在Schema中有NMTOKENS、IDREFS、ENTITIES三種內建的列表型,你也可以從已有的簡單型來創建list(列表)型,但你不能從已有的list型和複雜型來創建列表(list)型。 
如: 
<xsd:simpleType >
    <xsd:list itemType="myInteger"/>
</xsd:simpleType> 
在XML實例文檔中列表型的值是通過空格來進行分隔的,如果聲明瞭一個listOfMyIntType元素,其值可能是: 
<listOfMyInt>20003 15037 95977 95945</listOfMyInt> 

8、約束List類型。有幾個方面的元素可以應用於list型來進行約束,它們是:length、minLength、maxLength和enumeration,如: 
<xsd:simpleType >
    <xsd:list itemType="USState"/>
</xsd:simpleType>
<xsd:simpleType >
    <xsd:restriction base="USStateList">
        <xsd:length value="6"/>
    </xsd:restriction>
</xsd:simpleType> 
注:針對列表型要千萬注意成員是string型的,因爲string型中的空格和列表型的分割符空格會造成部分混淆。

9、對元素的定義可以採用通過指定其type屬性爲已定義的屬性的方式(基本類型或自定義的全局屬性),也可一採用匿名定義型的方式,如: 
採用型定義: 
<xsd:element name=”comment” type=”xsd:string”> 
採用匿名定義: 
<xsd:element
    <xsd:simpleType>
        <xsd:restriction base=”xsd:positiveInteger”>
            <xsd:maxExclusive value=”100” />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element> 

10、union(聯合)表示在XML實例文檔中的元素實例符合union型定義的成員型中的一種就可以了(合法),這一點和C++中的聯合型有似的概念,如: 
<xsd:simpleType >
    <xsd:union memberTypes="xsd:string integer"/>
</xsd:simpleType> 

11、複雜型一般可以分爲三
第一是包含字符內容和屬性但不包含子元素,針對第一可以通過simpleContent來實現;
第二是包含屬性和子元素但不包含字符數據(字符數據包含在子元素中),通過complexContent來做到;
第三是即包含屬性和字符內容又包含子元素的,只需要將complexType的屬性mixed設爲true就可以了;
具體的例子如下: 

第一種型(從一個簡單型擴展而來,增加了屬性): 
<xsd:element >
    <xsd:complexType>
        <xsd:simpleContent>
            <xsd:extension base="xsd:decimal">
                <xsd:attribute type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element> 

第二種型(有一個element和兩個attribute構成): 
<xsd:element >
    <xsd:complexType>
        <xsd:complexContent>
            <xsd:element ?type=”xsd:string” />
            <xsd:attribute type="xsd:string"/>
            <xsd:attribute ?type="xsd:decimal"/>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:element> 
注意:在這裏由於默認情況下缺省是complexContent,所以在這裏簡略的寫法是: 
<xsd:element >
    <xsd:complexType>
        <xsd:element />
        <xsd:attribute type="xsd:string"/>
        <xsd:attribute ?type="xsd:decimal"/>
    </xsd:complexType>
</xsd:element> 

第三種型: 
<xsd:element >
    <xsd:complexType mixed="true">
        <xsd:sequence>
            <xsd:element >
                <xsd:complexType mixed="true">
                    <xsd:sequence>
                        <xsd:element type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element type="xsd:positiveInteger"/>
            <xsd:element type="xsd:string"/>
            <xsd:element type="xsd:date" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element> 
第三種型的實例可能如下: 
<letterBody>
    <salutation>Dear Mr.
    <name>Robert Smith</name>.
    </salutation>
    Your order of
    <quantity>1</quantity>
    <productName>Baby Monitor</productName>
    shipped from our warehouse on
    <shipDate>1999-05-21</shipDate>
</letterBody>

12、根據11的描述那麼要定義一個空內容的元素,也就是說定義一個只包含屬性的元素,只要在complexContent中不包含任何子元素,就可以了,如(注:complexContent在complexType外面被省略了):
<xsd:element >
    <xsd:complexType>
        <xsd:attribute type="xsd:string"/>
        <xsd:attribute type="xsd:decimal"/>
    </xsd:complexType>
</xsd:element>

13、anyType是所有Schema型的基型,和Java中的Object似。因此,以下定義: 
<xsd:element name="anything" type="xsd:anyType"/> 
可以寫成: 
<xsd:element name="anything"/> 

14、Schema中用annotation、document、appInfo三個元素來進行註釋,其中appI和document都是作爲annotation的子元素來處理的。並且annotation一般是作爲schema的頂層子元素、element的構造、型定義的頂層子元素的。 
如: 
<xsd:element >
    <xsd:annotation>
        <xsd:documentation xml:lang="en">
            element declared with anonymous type
        </xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
        <xsd:annotation>
            <xsd:documentation xml:lang="en">
                empty anonymous type with 2 attributes
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexContent>
            <xsd:restriction base="xsd:anyType">
                <xsd:attribute type="xsd:string"/>
                <xsd:attribute type="xsd:decimal"/>
            </xsd:restriction>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:element> 

15、 choice,all
choice僅允許在實例文檔中使用其中一個子元素;在all中的所有元素都可以出現一次或一次都不出現,並且其中元素實例是沒有順序約束的,而且all 必須放在任何內容模型的最頂層,爲了說明這個問題,下面先列出一個合法的,然後列出一個不合法的以供對照說明: 
<xsd:complexType >
    <xsd:all>
        <xsd:element type="USAddress"/>
        <xsd:element type="USAddress"/>
        <xsd:element ref="comment" minOccurs="0"/>
        <xsd:element type="Items"/>
    </xsd:all>
    <xsd:attribute type="xsd:date"/>
</xsd:complexType> 
下面是一個不合法的: 
<xsd:complexType >
    <xsd:sequence>
        <xsd:all>
            <xsd:element type="USAddress"/>
            <xsd:element type="USAddress"/>
            <xsd:element type="Items"/>
        </xsd:all>
        <xsd:sequence>
            <xsd:element ref="comment" />
        </xsd:sequence>
    </xsd:sequence>
    <xsd:attribute type="xsd:date"/>
</xsd:complexType>


16、attributeGroup屬性組。在存在很多型中都有幾個相同的型的情況,可以採用屬性組的方式來進行重用,屬性組定義的格式是: 
<xsd:attributeGroup
    <xsd:attribute type=”xsd:string” />
    <xsd:attribute type=”xsd:string” />
    …
</xsd:attributeGroup> 

使用可以採用以下方式: 
<xsd:element
    <xsd:comlexType>
        <xsd:element />
        <xsd:attributeGroup ref=”attrGroupName” />
    </xsd:complexType>
</xsd:element> 

17、關於include的用法 
include元素可以將外部的定義和聲明引入到文檔中,並且作爲新Schema文檔的一部分,但必須注意的一點是,被包含成員的目標命名空間TargetNamespace必須和包含的目標命名空間schemaLocation一樣。具體寫法例子是: 
<include schemaLocation=“http://www.example.com/schemas/address.xsd” /> 

18、extension。如果一個型是從另一個型擴展而來的,那麼定義爲父型的element,在實例文檔中,可以通過符合子型的element實例來代替,但必須通過xsi:type指明此元素的具體型。例如: 
<xsd:complexType
    <xsd:sequence>
        <xsd:element type=”xsd:string” />
        <xsd:element type=”xsd:string” />
    </xsd:sequence>
</xsd:complexType> 
<!-- 擴展型定義 --> 
<xsd:complexType
    <complexContent>
        <xsd:extension base=”Person”>
            <xsd:sequence>
            <xsd:element
                <xsd:restriction base=”string”>
                    <xsd:enumeration value=”male” />
                </xsd:restriction>
            </xsd:element>
            </xsd:sequence>
        </xsd:extension>
    </complexContent>
</xsd:complexType>
<!-- 型的聲明 --> 
<xsd:element name=”human” type=”Person” /> 
在XML實例文檔中針對human可以是這樣的(和麪向對象有似的概念): 
<human xsi:type=”Father”>
    <name>xiaogen</name>
    <gender>male</gender>
</human> 
19、關於置換組 substituionGroup
XML Schema 提供了一種機制叫置換組,允許原先定義好的元素被其他元素所替換。更明確的,這個置換組包含了一系列的元素,這個置換組中的每一個元素都被定義爲可以替換 一個指定的元素,這個指定的元素稱爲頭元素(Head Element),需要注意的是頭元素必須作爲全局元素聲明,注意,當一個實例文檔包含置換元素時 替換元素的型時從它們的頭元素那裏派生的,此時並不需要使用我們前面所說的xsi:type來識別這些被派生的型,當定義了置換組之後,並非意味着不能使用頭元素,而只能使用這個置換組中的元素,它只是提供了一個允許元素可替換使用的機制。例如: 
<xsd:schema>
    <xsd:element type=”xsd:string”/>
    <xsd:element type=”xsd:string” substitutionGroup=”comment” />
    <xsd:element type=”xsd:string” substituionGroup=”comment” />
    <xsd:element
        <xsd:complexType>
            <xsd:element type=”xsd:string” />
            <xsd:element type=”xsd:decimal” />
            <xsd:element ref=”comment” />
            <xsd:element type=”xsd:date” />
        </xsd:complexType>
    </xsd:element>
</xsd:schema> 
下面是實例文檔的一個例子: 
<order>
    <productName>Lapis necklace</productName>
    <price>999</price>
    <shipComment>Use gold wrap if possible</shipComment>
    <customerComment>Want this for the holidays!</customerComment>
    <shipDate>2004-08-15</shipDate>
</order> 

20、抽象元素和抽象型 
當一個元素或者型被聲明爲“abstract”時,那麼它就不能在實例文檔中使用。當一個元素被聲明爲”abstract”的時候,元素的置換組的成員必須出現在實例文檔中。當一個元素相應的型被定義聲明爲"abstract"時,所有關聯該元素的實例必須使用"xsi:type"來指明一個型,這個型必須是非抽象的,同時是在定義中聲明的抽象型的派生型。 
一、如將上面的comment元素的聲明更改成: 
<xsd:element name=”comment” type=”xsd:string” abstract=”true” /> 
那麼上面的order實例中就只能包含customerComment和shipComment纔是有效的。 
二、如果有下面的型定義: 
<schema xmlns="http://www.w3.org/2001/XMLSchema"  targetNamespace="http://cars.example.com/schema"  xmlns:target="http://cars.example.com/schema"> 
    <complexType abstract="true"/> 
    <complexType > 
        <complexContent>
            <extension base="target:Vehicle"/>
        </complexContent>
    </complexType>
    <complexType > 
        <complexContent>
            <extension base="target:Vehicle"/>
        </complexContent>
    </complexType>
    <element type="target:Vehicle"/> 
</schema> 
根據以上的定義和聲明,下面的實例片斷就是不能通過驗證的: 
<transport xmlns="http://cars.example.com/schema" /> 
下面經過修改的就可以通過驗證了。 
<transport xmlns=“http://cars.example.com/schema”  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Car"/> 

21、Final。爲了阻止型被派生(包括通過限制派生和通過擴展派生),可以使用final來設置(有點和java中的final的概念似), 其值有三個:restriction,extension,#all。在模式文件的根元素schema元素中有一個可選的finalDefault屬性, 它的值能夠取爲final屬性所允許的幾個值之一。指定finalDefault屬性的值的效果等於在模式文檔中每個型定義和元素聲明中指定final屬性,同時其值爲finalDefault屬性的值。如果想阻止前面的Person被限制派生,我們需要修改定義爲如下: 
<xsd:complexType final=”restriction”>
    <xsd:sequence>
        <xsd:element type=”xsd:string” />
        <xsd:element type=”xsd:string” />
    </xsd:sequence>
</xsd:complexType> 

22、block。因爲在實例文檔中與父型關聯的元素(也就是聲明爲父型的元素)可以通過派生的子型來替代,這在2中已經有詳細的說明和例子。同時,XML Schema也提供了一種機制來控制派生型以及置換組在實例文檔中使用的機制。這種機制是通過型 的block屬性來控制的,該屬性可以取值爲:restriction,extension,#all。和final屬性一樣,在模式文檔的根元素 schema元素裏有一個可選的屬性blockDefault,它的值爲block屬性所允許的值中的一個。指定blockDefault屬性的作用等價 於在模式文檔中爲每個型定義和元素聲明指定block屬性。如在前面例子中我們想阻止在使用Father來替代Person的話我們需要修改Person的定義如下: 
<xsd:complexType block=”extension”>
    <xsd:sequence>
        <xsd:element type=”xsd:string” />
        <xsd:element type=”xsd:string” />
    </xsd:sequence>
</xsd:complexType> 
取值爲“extension”的block屬性將阻止在實例文檔中使用通過擴展的派生型來替換Person(即可以阻止Father來替換Person),然而它不會阻止通過約束的派生來替換Person。 

23、用fixed禁止派生。另外一種派生型的控制機制是應用於簡單型方面的派生。當定義一個簡單型的時候,我們可以使用fixed屬性對它的所有定義的參數進行修飾,以阻止這些參數在型派生中被修改,例如: 
<xsd:simpleType
    <xsd:restriction base=”xsd:string”>
        < xsd:length value=”7” fixed=”true” />
    </xsd:restriction>
</xsd:simpleType> 
當這個簡單型被定義之後,我們能夠派生出一個新的郵編型,在其中我們使用了一個沒有在基本型中固定的參數: 
<xsd:simpleType >
    <xsd:restriction base=" Postcode">
        <xsd:pattern value="[A-Z]{2}/d/s/d[A-Z]{2}"/>
    </xsd:restriction>
</xsd:simpleType> 
然而,我們不能購派生出一個這樣的新的郵編型:在其中我們重新定義了任何在基型中已經被固定(fixed)的參數: 
<xsd:simpleType >
    <xsd:restriction base="ipo:Postcode">
        <xsd:pattern value="[A-Z]{2}/d/d[A-Z]{2}"/>
        <!-- illegal attempt to modify facet fixed in base type --> 
        <xsd:length value="6" fixed="true"/>
    </xsd:restriction>
</xsd:simpleType>

 
發佈了31 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章