隨筆-95  評論-31  文章-10  trackbacks-0

                 (本文屬于自摸心得)XML Schema的作用是對XML文檔進行約束,其中包含許多內嵌數據類型并且自定義類型。SOAP標準、WSDL都使用XML Schema來進行數據類型格式的限制,如果想繼續深入使用webservice這些知識不可避免的要先學習,下面先從一個最簡單的XML開始,然后一步步擴展、增加復雜度,最后在寫出對應的XML Schema。

          1<?xml version="1.0"?>
          2<note>
          3<to>George</to>
          4<from>John</from>
          5<heading>Reminder</heading>
          6<body>Don't forget the meeting!</body>
          7</note>

          上面是個簡單的XM ,根元素是note、子元素是to、from、heading、body。那么對應的XML Schema:

           1<?xml version="1.0"?>
           2<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           3targetNamespace="http://www.w3school.com.cn"
           4xmlns="http://www.w3school.com.cn"
           5elementFormDefault="qualified">
           6
           7<xs:element name="note">
           8    <xs:complexType>
           9      <xs:sequence>
          10    <xs:element name="to" type="xs:string"/>
          11    <xs:element name="from" type="xs:string"/>
          12    <xs:element name="heading" type="xs:string"/>
          13    <xs:element name="body" type="xs:string"/>
          14      </xs:sequence>
          15    </xs:complexType>
          16</xs:element>
          17
          18</xs:schema>

          命名空間不必多說
          complexType表示:如果一個元素內部包含超過至少2個以上的元素,那么該元素就被認為是復雜類型元素。
          sequence表示:子元素按照順序依次列出。(這里的sequence可以替換為all、choice。all代表可以按照任意順序出現、choice代表選擇性即非此即彼)
          xs:string代表XML Schema的內置數據類型也不必多說即表示字符串,內置數據類型有許多種這里不一一列舉。
          如果根據這個XML Schema書寫XML即可寫成上面的XML文檔。
          如果note有屬性,即以下這種形式:

          1<?xml version="1.0"?>
          2<note name="must">
          3<to>George</to>
          4<from>John</from>
          5<heading>Reminder</heading>
          6<body>Don't forget the meeting!</body>
          7</note>
           那么與之對應的XML Schema為:
           1<?xml version="1.0"?>
           2<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           3targetNamespace="http://www.w3school.com.cn"
           4xmlns="http://www.w3school.com.cn"
           5elementFormDefault="qualified">
           6
           7<xs:element name="note">
           8    <xs:complexType>
           9      <xs:sequence>
          10    <xs:element name="to" type="xs:string"/>
          11    <xs:element name="from" type="xs:string"/>
          12    <xs:element name="heading" type="xs:string"/>
          13    <xs:element name="body" type="xs:string"/>
          14      </xs:sequence>
          15    <xs:attribute name="must" type="xs:string" use="required">
          16    </xs:complexType>
          17</xs:element>
          18
          19</xs:schema>
          其中屬性緊跟著定義在元素之后(sequence與complextype之間),use="required"表示該屬性必須出現,即must
          如果note里面的子元素比如to要出現兩次(表示不但要給喬治說也要給老大說。。。),XML如下所示:
          1<?xml version="1.0"?>
          2<note name="must">
          3<to>George</to>
          4<to>老大</to>
          5<from>John</from>
          6<heading>Reminder</heading>
          7<body>Don't forget the meeting!</body>
          8</note>
          那么對應的XML Schema約束可以為:
           1<?xml version="1.0"?>
           2<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           3targetNamespace="http://www.w3school.com.cn"
           4xmlns="http://www.w3school.com.cn"
           5elementFormDefault="qualified">
           6
           7<xs:element name="note">
           8    <xs:complexType>
           9      <xs:sequence>
          10    <xs:element name="to" type="xs:string"  maxOccurs="2" minOccurs="1"/>
          11    <xs:element name="from" type="xs:string"/>
          12    <xs:element name="heading" type="xs:string"/>
          13    <xs:element name="body" type="xs:string"/>
          14      </xs:sequence>
          15<xs:attribute name="must" use="required"/>
          16    </xs:complexType>
          17</xs:element>
          18
          19</xs:schema>
          其中maxOccurs表示最多出現2次,minOccurs最少出現1次

          未完待續。。。
          posted on 2011-03-17 02:26 朔望魔刃 閱讀(345) 評論(1)  編輯  收藏

          評論:
          # re: XML Schema 2013-01-13 17:35 | W3School官網

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 七台河市| 呈贡县| 外汇| 汉寿县| 鄂尔多斯市| 周口市| 安义县| 六安市| 南通市| 保亭| 宜宾县| 胶南市| 赣州市| 吴忠市| 崇明县| 娄底市| 图片| 崇仁县| 石林| 舒兰市| 永丰县| 黄石市| 达州市| 板桥市| 鹿泉市| 满城县| 临汾市| 交城县| 图片| 五家渠市| 曲麻莱县| 昭苏县| 平安县| 永德县| 醴陵市| 孝感市| 安义县| 林周县| 云浮市| 积石山| 梓潼县|