XML Schema學習筆記

來源:互聯網
上載者:User

XML Schema學習筆記

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

<xsd:complexType name="CNAddress" >

 <xsd:sequence>

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

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

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

   <xsd:element name="zip"    type="xsd:decimal"/>

 </xsd:sequence>

 <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>

 </xsd:complexType>

 

2、element存在約束:element可以通過其minOccurs和maxOccurs兩個屬性來約束元素執行個體存在的個數,這兩個屬性的預設值都是1,表示預設情況下此元素在XML執行個體文檔中必須出現一次。

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兩個屬性不能同時存在,否則會出現錯誤。

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

6、派生新類型有兩種方式:第一種就是直接從其他類型中擴充(繼承)而來,另外一種就是通過對已有類型進行限定性約束而來。

   如:以下有三種通過限定性約束定義的新類型:

 通過值範圍限定:

<xsd:simpleType name="myInteger">

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

    <xsd:minInclusive value="10000"/>

    <xsd:maxInclusive value="99999"/>

 </xsd:restriction>

</xsd:simpleType>

 使用模式比對限定:

<xsd:simpleType name="SKU">

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

    <xsd:pattern value="\d{3}-[A-Z]{2}"/>

 </xsd:restriction>

</xsd:simpleType>

 使用枚舉方式限定:

<xsd:simpleType name="CNCity">

 <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 name="listOfMyIntType">

 <xsd:list itemType="myInteger"/>

</xsd:simpleType>

在XML執行個體文檔中清單類型的值是通過空格來進行分隔的,如果聲明了一個listOfMyIntType元素,其值可能是:

<listOfMyInt>20003 15037 95977 95945</listOfMyInt>

8、有幾個方面的元素可以應用於list類型來進行約束,它們是:length、minLength、maxLength和enumeration,如:

<xsd:simpleType name="USStateList">

      <xsd:list itemType="USState"/>

</xsd:simpleType>

 

<xsd:simpleType name="SixUSStates">

  <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 name=”quantity”>

      <xsd:simpleType>

              <xsd:restriction base=”xsd:positiveInteger”>

                    <xsd:maxExclusive value=”100” />

              </xsd:restriction>

      </xsd:simpleType>

</xsd:element>

10、union(聯合)類型表示在XML執行個體文檔中的元素執行個體符合union類型定義的成員類型中的一種就可以了(合法),這一點和C++中的等位型別有類似的概念,如:

<xsd:simpleType name="addrUnion">

  <xsd:union memberTypes="xsd:string integer"/>

</xsd:simpleType>

11、複雜類型一般可以分為三類:第一類是包含字元內容和屬性但不包含子項目;第二類是包含屬性和子項目但不包含字元資料(字元資料包含在子項目中);第三類是即包含屬性和字元內容又包含子項目的;那麼如何來定義這三類類型呢?針對第一類可以通過simpleContent來實現,第二類可以通過complexContent來做到,第三類只需要將complexType的屬性mixed設為true就可以了。具體的例子如下:

第一種類型(從一個簡單類型擴充而來,增加了屬性):

<xsd:element name="internationalPrice">

 <xsd:complexType>

   <xsd:simpleContent>

    <xsd:extension base="xsd:decimal">

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

    </xsd:extension>

   </xsd:simpleContent>

 </xsd:complexType>

</xsd:element>

 第二種類型(有一個element和兩個attribute構成):

<xsd:element name="internationalPrice">

 <xsd:complexType>

 <xsd:complexContent>

    <xsd:element name=”Country”     type=”xsd:string” />

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

    <xsd:attribute name="value"     type="xsd:decimal"/>

   </xsd:complexContent>

 </xsd:complexType>

</xsd:element>

 注意:在這裡由於預設情況下預設是complexContent,所以在這裡簡略的寫法是:

<xsd:element name="internationalPrice">

 <xsd:complexType>

    <xsd:element name=”Country”     type=”xsd:string” />

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

    <xsd:attribute name="value"     type="xsd:decimal"/>

   </xsd:complexContent>

</xsd:element>

 第三種類型:

<xsd:element name="letterBody">

 <xsd:complexType mixed="true">

 <xsd:sequence>

   <xsd:element name="salutation">

    <xsd:complexType mixed="true">

     <xsd:sequence>

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

     </xsd:sequence>

    </xsd:complexType>

   </xsd:element>

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

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

   <xsd:element name="shipDate"    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中不包含任何子項目,就可以了,如:

<xsd:element name="internationalPrice">

 <xsd:complexType>

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

 <xsd:attribute name="value"    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 name="internationalPrice">

 <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 name="currency" type="xsd:string"/>

    <xsd:attribute name="value"    type="xsd:decimal"/>

   </xsd:restriction>

 </xsd:complexContent>

 </xsd:complexType>

</xsd:element>

15、choice僅允許在執行個體文檔中使用其中一個子項目;在all中的所有元素都可以出現一次或一次都不出現,並且其中元素執行個體是沒有順序約束的,而且all必須放在任何內容模型的最頂層,為了說明這個問題,下面先列出一個合法的,然後列出一個不合法的以供對照說明:

<xsd:complexType name="PurchaseOrderType">

 <xsd:all>

    <xsd:element name="shipTo" type="USAddress"/>

    <xsd:element name="billTo" type="USAddress"/>

    <xsd:element ref="comment" minOccurs="0"/>

    <xsd:element name="items" type="Items"/>

 </xsd:all>

 <xsd:attribute name="orderDate" type="xsd:date"/>

</xsd:complexType>

 下面是一個不合法的:

<xsd:complexType name="PurchaseOrderType">

 <xsd:sequence>

 <xsd:all>

    <xsd:element name="shipTo" type="USAddress"/>

    <xsd:element name="billTo" type="USAddress"/>

    <xsd:element name="items" type="Items"/>

 </xsd:all>

 <xsd:sequence>

   <xsd:element ref="comment" />

 </xsd:sequence>

 </xsd:sequence>

 <xsd:attribute name="orderDate" type="xsd:date"/>

</xsd:complexType>

16、在存在很多類型中都有幾個相同的類型的情況,可以採用屬性群組的方式來進行重用,屬性群組定義的格式是:

<xsd:attributeGroup name=”attrGroupName”>

      <xsd:attribute name=”attrName1” type=”xsd:string” />

      <xsd:attribute name=”attrName2” type=”xsd:string” />

      …

</xsd:attributeGroup>

 使用可以採用以下方式:

<xsd:element name=”testAttrGroup”>

    <xsd:comlexType>

        <xsd:element name=”element1” type=”xsd:string” />

        <xsd:attributeGroup ref=”attrGroupName” />

    </xsd:complexType>

</xsd:element>

聯繫我們

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