yeshucheng
          追逐自己,追逐方向,心隨悟所動
          posts - 24,comments - 24,trackbacks - 0

          Xml Schema的用途

          1. 定義一個Xml文檔中都有什么元素

          2. 定義一個Xml文檔中都會有什么屬性

          3. 定義某個節點的都有什么樣的子節點,可以有多少個子節點,子節點出現的順序

          4. 定義元素或者屬性的數據類型

          5. 定義元素或者屬性的默認值或者固定值

          Xml Schema的根元素:

          <?xml version="1.0"?>

          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 表示數據類型等定義來自w3

          targetNamespace="http://www.w3schools.com" 表示文檔中要定義的元素來自什么命名空間

          xmlns="http://www.w3schools.com"表示此文檔的默認命名空間是什么

          elementFormDefault="qualified"> 表示要求xml文檔的每一個元素都要有命名空間指定

          ……定義主體部分……

          </xs:schema>

          如何定義一個簡單元素

          <xs:element  此處表示要定義一個元素

          name=”color” 表示要定義元素的名稱

          type=”xs:string” 表示要定義元素的數據類型

          default=”red” 表示定義元素的默認值

          fixed=”red”/> 表示要定義元素的固定值,此元素只可以取“red”值

          以上定義了一個簡單元素,元素實例:<color>red</color>

          如何定義一個屬性

          <xs:attribute

                   name=”birthday” 表示要定義屬性的名字

                   type=”xs:date” 表示要定義屬性的數據類型

                   default=”2001-01-11” 表示要定義屬性的默認值

                   fixed=”2001-01-11” 表示要定義屬性的固定值

                   use=”required”/> 表示此屬性是否是必須指定的,即如果不指定就不符合Schema,默認沒有use=”required”屬性表示屬性可有可無

          如何定義元素或者屬性值的限制

          1.最大值最小值限制

          <xs:element name="age">

          <xs:simpleType>

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

          <xs:minInclusive value="0"/> 大于等于0<xs: minExclusive>表示最小值但是不包括指定值

           <xs:maxInclusive value="120"/> 小于等于120<xs: maxExclusive>

          </xs:restriction>

          </xs:simpleType>

          </xs:element>

          2.枚舉限制,指只能在指定的幾個值中取值

                   <xs:element name="car" type="carType"/>

          <xs:simpleType name="carType">

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

              <xs:enumeration value="Audi"/>

              <xs:enumeration value="Golf"/>

              <xs:enumeration value="BMW"/>

           </xs:restriction>

          </xs:simpleType>

          3.模式(pattern)限制,指字符串的格式必須滿足制定的匹配模式

          例子

          說明

          <xs:element name="letter">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="[a-z]"/>
           </xs:restriction>
          </xs:simpleType>

          </xs:element>

          表示只能在小寫字母中取一個值

          <xs:element name="initials">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="[A-Z][A-Z][A-Z]"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示必須是三個大寫字母

          <xs:element name="initials">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示必須是三個字母,可以是大寫或小寫的

          <xs:element name="choice">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="[xyz]"/>
           </xs:restriction>
          </xs:simpleType>

          </xs:element>

          表示必須是xyz中的一個

          <xs:element name="prodid">
          <xs:simpleType>
           <xs:restriction base="xs:integer">
              <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示數字的范圍是0-99999

          <xs:element name="letter">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="([a-z])*"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示必須是0或者多個小寫字符組成的序列

          <xs:element name="letter">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="([a-z][A-Z])+"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示必須是多個字母。

          <xs:element name="gender">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="male|female"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示是male或者female中的一個

          <xs:element name="password">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:pattern value="[a-zA-Z0-9]{8}"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          表示必須是8個字母數字字符

          4.字符串長度的限制

          <xs:element name="password">

          <xs:simpleType>

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

              <xs:length value="8"/>

           </xs:restriction>

          </xs:simpleType>

          </xs:element>

          長度必須是8

          <xs:element name="password">

          <xs:simpleType>

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

              <xs:minLength value="5"/>

              <xs:maxLength value="8"/>

           </xs:restriction>

          </xs:simpleType>

          </xs:element>

          表示長度在5-8之間

          6.對于空白字符的限制

          示例

          說明

          <xs:element name="address">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:whiteSpace value="preserve"/>
           </xs:restriction>
          </xs:simpleType>

          </xs:element>

          保留原樣,表示xml處理器不會移除或者替換任何空白字符

          <xs:element name="address">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:whiteSpace value="replace"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          指回車,換行,Tab都會被替換成空格處理

          <xs:element name="address">
          <xs:simpleType>
           <xs:restriction base="xs:string">
              <xs:whiteSpace value="collapse"/>
           </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          去掉多于一個空格,和html中處理方式相同

          如何定義復雜類型

          復雜類型是指定義元素中包含屬性或者子元素的類型

          1.定義只包含子元素的復雜類型

          <xs:element name="person">

           <xs:complexType>

              <xs:sequence>

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

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

              </xs:sequence>

           </xs:complexType>

          </xs:element>

          2.定義只包含屬性的復雜類型

          <xs:element name="product" type="prodtype"/>

          <xs:complexType name="prodtype">

           <xs:attribute name="prodid" type="xs:positiveInteger"/>

          </xs:complexType>

          3.定義只包含內容的復雜類型

          <xs:element name="shoesize" type="shoetype"/>

          <xs:complexType name="shoetype">

           <xs:simpleContent>

              <xs:extension base="xs:integer">

                <xs:attribute name="country" type="xs:string" />

              </xs:extension>

           </xs:simpleContent>

          </xs:complexType>

          4.定義包含內容和子元素混合的復雜類型

          <xs:element name="letter">

           <xs:complexType mixed="true">

              <xs:sequence>

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

                <xs:element name="orderid" type="xs:positiveInteger"/>

                <xs:element name="shipdate" type="xs:date"/>

              </xs:sequence>

           </xs:complexType>

          </xs:element>

          以上定義對應的Xml

          <letter>

          Dear Mr.<name>John Smith</name>.

          Your order <orderid>1032</orderid>

          will be shipped on <shipdate>2001-07-13</shipdate>.

          </letter>

          5.定義包含屬性和子元素的復雜類型

          使用指示器

          Xsd中的指示器包括

          1.順序指示器

          1)All

             英文解釋:The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once
          規定子元素能夠以任意順序出現,每個子元素必須只出現一次。

          <xs:element name="person">

           <xs:complexType>

              <xs:all>

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

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

              </xs:all>

           </xs:complexType>

          </xs:element>

          2)Choice

          指示子元素中可以出現一個或者另一個

          <xs:element name="person">

           <xs:complexType>

              <xs:choice>

                <xs:element name="employee" type="employee"/>

                <xs:element name="member" type="member"/>

              </xs:choice>

           </xs:complexType>

          </xs:element>

          3)Sequence

          指示子元素必須按照順序出現

          <xs:element name="person">

           <xs:complexType>

              <xs:sequence>

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

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

              </xs:sequence>

           </xs:complexType>

          </xs:element>

          2.出現次數指示器minOccursmaxOccurs

          <xs:element name="person">

           <xs:complexType>

              <xs:sequence>

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

                <xs:element name="child_name" type="xs:string"

                maxOccurs="10" minOccurs="0"/>

              </xs:sequence>

           </xs:complexType>

          </xs:element>

          3.組指示器(group Indicators

          用來定義相關的一組元素

          <xs:group name="persongroup">

           <xs:sequence>

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

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

              <xs:element name="birthday" type="xs:date"/>

           </xs:sequence>

          </xs:group>

          <xs:element name="person" type="personinfo"/>

          <xs:complexType name="personinfo">

           <xs:sequence>

             <xs:group ref="persongroup"/>

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

           </xs:sequence>

             </xs:complexType>

          用來定義一組相關的屬性

          <xs:attributeGroup name="personattrgroup">

           <xs:attribute name="firstname" type="xs:string"/>

           <xs:attribute name="lastname" type="xs:string"/>

           <xs:attribute name="birthday" type="xs:date"/>

          </xs:attributeGroup>

          <xs:element name="person">

           <xs:complexType>

              <xs:attributeGroup ref="personattrgroup"/>

           </xs:complexType>

          </xs:element>

          Any關鍵字

          表示可以有任意元素

          <xs:element name="person">

           <xs:complexType>

              <xs:sequence>

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

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

                <xs:any minOccurs="0"/>

              </xs:sequence>

           </xs:complexType>

          </xs:element>

          anyAttribute關鍵字

          <xs:element name="person">

           <xs:complexType>

              <xs:sequence>

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

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

              </xs:sequence>

              <xs:anyAttribute/>

           </xs:complexType>

          </xs:element>

          substitutionGroup關鍵字

          表示某一個元素和另一個替代元素定義相同

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

          <xs:element name="navn" substitutionGroup="name"/>

          <xs:complexType name="custinfo">

           <xs:sequence>

              <xs:element ref="name"/>

           </xs:sequence>

          </xs:complexType><xs:element name="customer" type="custinfo"/>

          <xs:element name="kunde" substitutionGroup="customer"/>

          文中的例子都來自w3school.

          posted on 2009-02-24 11:18 葉澍成 閱讀(392) 評論(0)  編輯  收藏
          主站蜘蛛池模板: 东丰县| 湖口县| 龙岩市| 洪江市| 崇礼县| 皮山县| 南丰县| 阿勒泰市| 确山县| 泾川县| 姜堰市| 蒲城县| 徐汇区| 修水县| 永春县| 靖安县| 宁安市| 密云县| 卓资县| 台山市| 堆龙德庆县| 沾益县| 吉林市| 衢州市| 武隆县| 白山市| 华宁县| 根河市| 迭部县| 南江县| 枣阳市| 广饶县| 宣城市| 肥西县| 扎囊县| 寻甸| 应城市| 桐城市| 浏阳市| 台南市| 河津市|