XML Schema學習筆記
1、復雜類型和簡單類型之間最根本的區(qū)別就是:復雜類型的內容中可以包含其他元素,也可以帶有屬性(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三個值,缺?。J)值是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等系統(tǒng)內建的類型)、列表類型、聯合類型合起來統(tǒng)一稱為簡單類型。在Schema中有NMTOKENS、IDREFS、ENTITIES三種內建的列表類型,你也可以從已有的簡單類型來創(chuàng)建list(列表)類型,但你不能從已有的list類型和復雜類型來創(chuàng)建列表(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&
關鍵詞:XML,Schema,學習筆記
閱讀本文后您有什么感想? 已有 人給出評價!
- 13
- 13
- 13
- 13
- 13
- 13