欢迎大家访问我的BLOG,我会多多的出原创文章,希望大家支持我,为我祈祷,让我实现我的三个梦想!再30岁能成为一个名优秀的软件架构师!

跟我学XML Schema(第6-11天)

六:跟我学XML Schema(6):如何定义可选项的子元素?

 假如上面的订书数据中,可以用书名或者书号任一一种订购,则实例文档可能如下:

order2.xml
-----------------
<order>
  <orderItem>
    <!--书名订购-->
    <name>Accounting Book</name>
  </orderItem>
  <orderItem>
    <!--书号订购-->
    <id>7-5058-3496-7</id>
  </orderItem>
</order>

这时书写Schema文档还需要使用choice元素。

order2.xsd
-------------------------
1:<?xml version="1.0"?>
2:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4:  <xsd:element name="order">
5:    <xsd:complexType>
6:      <xsd:sequence>
7:        <xsd:element ref="orderItem" maxOccurs="10" />
8:      </xsd:sequence>
9:    </xsd:complexType>
10:  </xsd:element>
11:
12:  <xsd:element name="orderItem">
13:    <xsd:complexType>
14:      <xsd:choice>
15:        <xsd:element name="name" type="xsd:string"/>
16:        <xsd:element name="id" type="xsd:string"/>
17:      </xsd:choice>
18:    </xsd:complexType>
19:  </xsd:element>
20:
21:</xsd:schema>

七:跟我学XML Schema(7):稍微更复杂的可选项子元素

 再稍微修改一下订书数据的实例文档:

order3.xml
-----------------
<order>
  <orderItem>
    <name>Accounting Book</name>
    <quantity>2</quantity>
  </orderItem>
  <orderItem>
    <id>7-5058-3496-7</id>
  </orderItem>
</order>

这里假定<quantity>值为1时,缺省。

如何修改Schema文档呢?

order3.xsd
-----------------
1:<?xml version="1.0"?>
2:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4:  <xsd:element name="order">
5:    <xsd:complexType>
6:      <xsd:sequence>
7:        <xsd:element ref="orderItem" maxOccurs="10"/>
8:      </xsd:sequence>
9:    </xsd:complexType>
10:  </xsd:element>
11:
12:  <xsd:element name="orderItem">
13:    <xsd:complexType>
14:      <xsd:sequence>
15:        <xsd:choice>
16:          <xsd:element name="name" type="xsd:string"/>
17:          <xsd:element name="id" type="xsd:string"/>
18:        </xsd:choice>
19:        <xsd:element name="quantity" type="xsd:string" minOccurs="0"/>
20:      </xsd:sequence>
21:    </xsd:complexType>
22:  </xsd:element>
23:
24:</xsd:schema>

19行中的quantity最少出现值为0,也就是可以有,也可以没有。
当然,也可以直接在<choice>元素中,包含quantity,然后定义它的minOccurs。

八:跟我学XML Schema(8):内置简单类型

 

内建于XML Schema的简单类型有44种。他们在XML Schema推荐标准的第二部分中公布,下面这是一张内置类型的层次结构图:

http://www.w3.org/TR/2001/PR-xmlschema-2-20010330/type-hierarchy.jpg

九:跟我学XML Schema(9):自定义简单类型

 

如果内置简单类型的44种还不能满足要求,怎么办呢?下面学习自定义简单类型。(XML的扩展性充分体现在这里)

例如这个实例文档:

order4.xml
-----------------
<order>
  <orderItem>
    <id>7-5058-3496-7</id>
    <quantity>5</quantity>
  </orderItem>
</order>

ID是一个标准的ISBN编码,我们怎么定义这个ISBN编码呢?

<xsd:simpleType name="idType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>
  </xsd:restriction>
</xsd:simpleType>

idType是一个自定义的简单类型。
我们对它做了限制:
<xsd:restriction base="xsd:string">代表它是基于一个字符串类型。再用pattern元素来描述该字符串的形式。

value="\d{1}-\d{4}-\d{4}-\d{1}"这是一个正则表达式,关于正则表达式,以后再介绍。嘻嘻!

利用这个自定义的简单类型,我们可以重新写Schema文档:

order4.xsd
---------------
1:<?xml version="1.0"?>
2:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4:  <xsd:element name="order">
5:    <xsd:complexType>
6:      <xsd:sequence>
7:        <xsd:element ref="orderItem" maxOccurs="10"/>
8:      </xsd:sequence>
9:    </xsd:complexType>
10:  </xsd:element>
11:
12:  <xsd:element name="orderItem">
13:    <xsd:complexType>
14:      <xsd:sequence>
15:        <xsd:element name="id" type="idType"/>
16:        <xsd:element name="quantity" type="xsd:integer"/>
17:      </xsd:sequence>
18:    </xsd:complexType>
19:  </xsd:element>
20:  
21:  <xsd:simpleType name="idType">
22:    <xsd:restriction base="xsd:string">
23:      <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>
24:    </xsd:restriction>
25:  </xsd:simpleType>
26:  
27:</xsd:schema>


假如我们事先确定好ID只有3个,即只有3个ISBN是可选的,那怎么办?我们可以用enumeration元素来进行列举。

<xsd:simpleType name="idType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="7-5058-3496-7"/>
    <xsd:enumeration value="7-5005-6450-3"/>
    <xsd:enumeration value="7-3020-6069-7"/>
  </xsd:restriction>
</xsd:simpleType>


再来看订购量quantity的值,如果我们设定其值必须在1-10之间,该怎么办呢?可以这些自定义一个简单类型。

<xsd:simpleType name="quantityType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="1"/>
    <xsd:maxInclusive value="10"/>
  </xsd:restriction>
</xsd:simpleType>

其中,minInclusive,maxInclusive分别代表该类型的取值范围。

所以最终修改后的Schema文档如下:

order4-1.xsd
----------------------
1:<?xml version="1.0"?>
2:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4:  <xsd:element name="order">
5:    <xsd:complexType>
6:      <xsd:sequence>
7:        <xsd:element ref="orderItem" maxOccurs="10"/>
8:      </xsd:sequence>
9:    </xsd:complexType>
10:  </xsd:element>
11:
12:  <xsd:element name="orderItem">
13:    <xsd:complexType>
14:      <xsd:sequence>
15:        <xsd:element name="id" type="idType"/>
16:        <xsd:element name="quantity" type="quantityType"/>
17:      </xsd:sequence>
18:    </xsd:complexType>
19:  </xsd:element>
20:  
21:  <xsd:simpleType name="idType">
22:    <xsd:restriction base="xsd:string">
23:      <xsd:enumeration value="7-5058-3496-7"/>
    <xsd:enumeration value="7-5005-6450-3"/>
    <xsd:enumeration value="7-3020-6069-7"/>
26:    </xsd:restriction>
27:  </xsd:simpleType>
28:  
29:  <xsd:simpleType name="quantityType">
30:    <xsd:restriction base="xsd:integer">
31:      <xsd:minInclusive value="1"/>
32:      <xsd:maxInclusive value="10"/>
33:    </xsd:restriction>
34:  </xsd:simpleType>
35:
36:</xsd:schema>

十:跟我学XML Schema(10):定义属性

 

最后,我们再来讲讲元素的属性如何在Schema文档中定义。

比如上面的order.xml实例文档中:

<order>
  <orderItem id="7-5058-3496-7" />
</order>

对此,我们在Schema文档中采用一个attribute来定义:

order.xsd
---------
<xsd:element name="orderItem">
  <xsd:complexType>
    <xsd:sequence>  ←空元素
    </xsd:sequence>  
    
    <!--定义该元素属性-->
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
</xsd:element>

那么,实例文档中该属性值是必须的还是可有可无的呢?我们可以这样限制:

<xsd:attribute name="id" type="idType" use="required"/>

这里我们讲id属性类型作为一种自定义数据类型idType。
而且,用attribute元素的use属性来定义是否是必须的属性。
required是必须值,optional是可选值,prohibited是无属性值。


那么对于属性的缺省值,我们怎么定义呢?
比如:
<order>
  <orderItem id="4-8443-1780-6" quantity="3"/>
</order>

我们还可以用attribute元素的另一个属性default来定义:
<xsd:attribute name="quantity" type="xsd:integer" default="1"/>

所以,我们可以重新写出一个Schema文档:

order2.xsd
--------------
<xsd:element name="orderItem">
  <xsd:complexType>
    <xsd:sequence></xsd:sequence>
    <xsd:attribute name="id" type="idType" use="required"/>
    <xsd:attribute name="quantity" type="xsd:integer" default="1"/>
  </xsd:complexType>
</xsd:element>

上面的属性我们定义我们还可以采用属性组的办法来重新改写Schema文档。

 

order3.xsd
----------------
1:    <xsd:element name="orderItem">
2:      <xsd:complexType>
3:        <xsd:sequence></xsd:sequence>
4:        <xsd:attributeGroup ref="orderItemAttributes"/>
5:      </xsd:complexType>
6:    </xsd:element>
7:
8:    <xsd:attributeGroup name="orderItemAttributes">
9:      <xsd:attribute name="id" type="idType" use="required"/>
10:      <xsd:attribute name="quantity" type="xsd:integer" default="1"/>
11:    </xsd:attributeGroup>

这个属性组就不详细解释了,不过,大家一看就清楚了吧。

最后,我们写一个完整的订书order.xml的Schema文档。
1:  <?xml version="1.0"?>
2:  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4:    <xsd:element name="order">
5:      <xsd:complexType>
6:        <xsd:sequence>
7:          <xsd:element ref="orderItem" maxOccurs="10"/>
8:        </xsd:sequence>
9:      </xsd:complexType>
10:    </xsd:element>
11:
12:    <xsd:element name="orderItem">
13:      <xsd:complexType>
14:        <xsd:sequence></xsd:sequence>
15:        <xsd:attributeGroup ref="orderItemAttributes"/>
16:      </xsd:complexType>
17:    </xsd:element>
18:
19:    <xsd:attributeGroup name="orderItemAttributes">
20:      <xsd:attribute name="id" type="idType" use="required"/>
21:      <xsd:attribute name="quantity" type="xsd:integer" default="1"/>
22:    </xsd:attributeGroup>
23:
24:    <xsd:simpleType name="idType">
25:      <xsd:restriction base="xsd:string">
26:        <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>
27:      </xsd:restriction>
28:    </xsd:simpleType>
29:
30:  </xsd:schema>



网友补充:
   1、element和attribute属性中都有一个default属性和fixed属性,注意这两个属性不能同时存在。
   2、原子类型(不可分割的类型,象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>
  3、有几个方面的元素可以应用于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类型中的空格和列表类型的分割符空格会造成部分混淆。
  4、union(联合)类型表示在XML实例文档中的元素实例符合union类型定义的成员类型中的一种就可以了(合法),这一点和C++中的联合类型有类似的概念,如:
     <xsd:simpleType name="addrUnion">
    <xsd:union memberTypes="xsd:string integer"/>
    </xsd:simpleType>

posted on 2006-08-11 23:42  程序缘  阅读(353)  评论(0)    收藏  举报

导航