理解XML Schema: XML Schema初步 (七)

来源:互联网 发布:cf个人仓库查询软件 编辑:程序博客网 时间:2024/06/08 05:58

十七属性组

 

 

假设我们想在购买定单里面为每个物品表示更多的信息,例如,每个物品的重量和期望的运输方式。我们希望能够通过为item元素的(匿名)类型定义添加weightKg和shipBy属性声明来做到这点(参见下图)。

 

<xsd:element name="Item" minOccurs="0" maxOccurs="unbounded">

  <xsd:complexType>

   <xsd:sequence>

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

    <xsd:element   name="quantity">

     <xsd:simpleType>

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

       <xsd:maxExclusive value="100"/>

      </xsd:restriction>

     </xsd:simpleType>

    </xsd:element>

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

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

    <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>

   </xsd:sequence>

   <xsd:attribute name="partNum"  type="SKU" use="required"/>

   <!-- add weightKg and shipBy attributes -->

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

   <xsd:attribute name="shipBy">

    <xsd:simpleType>

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

      <xsd:enumeration value="air"/>

      <xsd:enumeration value="land"/>

      <xsd:enumeration value="any"/>

     </xsd:restriction>

    </xsd:simpleType>

   </xsd:attribute>

  </xsd:complexType>

</xsd:element>

 

 

 

或者,我们可以建立一个被命名的属性组来包含所有item元素所期望的属性,并且在item元素声明中通过名字来引用这个属性组(参见下图):

 

<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">

 <xsd:complexType>

  <xsd:sequence>

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

   <xsd:element name="quantity">

    <xsd:simpleType>

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

      <xsd:maxExclusive value="100"/>

     </xsd:restriction>

    </xsd:simpleType>

   </xsd:element>

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

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

   <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>

  </xsd:sequence>

  <!-- attributeGroup replaces individual declarations -->

  <xsd:attributeGroup ref="ItemDelivery"/>

 </xsd:complexType>

</xsd:element>

<xsd:attributeGroup name="ItemDelivery">

  <xsd:attribute name="partNum"  type="SKU" use="required"/>

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

  <xsd:attribute name="shipBy">

    <xsd:simpleType>

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

      <xsd:enumeration value="air"/>

      <xsd:enumeration value="land"/>

      <xsd:enumeration value="any"/>

     </xsd:restriction>

    </xsd:simpleType>

  </xsd:attribute>

</xsd:attributeGroup>

 

 

 

通过这种方法来使用属性组,可以提高模式文档的可读性,同时也便于更新模式文档。这是因为一个属性组能够在一个地方定义和编辑,同时能够在多个定义和声明中被引用。注意到一个属性组可以包含其他属性组,同时还要注意到属性组的声明和引用必须在复合类型定义的最后。

 

 

 

 

十八空值(Nil)

 

 

我们再回到前面的po.xml来,这个购买定单中购买的物品之一Lawnmower,是没有shipDate元素的。在我们的这个应用背景中,模式文档和实例文档的作者可能故意安排这样的缺席用来表示这个item还没有被运出。但是,一般的来说,缺少一个元素并没有任何特别的意义:它也许表示信息不可知或者不适用或者因为其他的原因而不存在。有时常常是通过增加一个元素而不是通过缺少一个元素,来明确地表达关于未运出的物品、未知信息或者不适用信息等。举例来说,也许想要使用一个元素来表示发送空值或者表示数据库中的空值,类似的情况可以使用XML Shema的空值机制来表现,这个机制允许一个元素以空值或者非空值出现。

 

<?xml version="1.0"?>

<purchaseOrder orderDate="1999-10-20">

    <shipTo country="US">

        <name>Alice Smith</name>

        <street>123 Maple Street</street>

        <city>Mill Valley</city>

        <state>CA</state>

        <zip>90952</zip>

    </shipTo>

    <billTo country="US">

        <name>Robert Smith</name>

        <street>8 Oak Avenue</street>

        <city>Old Town</city>

        <state>PA</state>

        <zip>95819</zip>

    </billTo>

    <comment>Hurry, my lawn is going wild!</comment>

    <items>

        <item partNum="872-AA">

            <productName>Lawnmower</productName>

            <quantity>1</quantity>

            <USPrice>148.95</USPrice>

            <comment>Confirm this is electric</comment>

        </item>

        <item partNum="926-AA">

            <productName>Baby Monitor</productName>

            <quantity>1</quantity>

            <USPrice>39.98</USPrice>

            <shipDate>1999-05-21</shipDate>

        </item>

    </items>

</purchaseOrder>

 

 

 

XML Schema 空值机制包括一个空值信号。换句话说,作为元素内容而言,并没有没有真正的空值,代之的是一个说明元素的内容是空值的属性。为了显示这点,我们修改shipDate元素的声明,这样空值就能够被明确地告知用户了。 <xsd:element name="shipDate" type="xsd:date" nillable="true"/>

 

为了在实例文档中明确的表示shipDate有一个空值,我们可以设置nil属性为真: <shipDate xsi:nil="true"></shipDate>

 

nil属性是作为XML Schema命名空间的一部分来定义的,即"http://www.w3.org/2001/XMLSchema-instance",并且在实例文档中必须带有与命名空间相对应的前缀(一般定义为xsi:)出现。需要注意的是,空值机制仅仅适用于元素值,而不适用于属性值,一个元素有xsi:nil="true"可以没有任何元素内容但仍旧可以带有其他属性。

原创粉丝点击