如何在XSD文件中为子元素添加唯一ID?

时间:2013-02-11 23:44:48

标签: xsd

我无法弄清楚如何创建我的XSD文件以使子元素具有唯一ID。它适用于根元素,但复制此代码并更改值不起作用,所以我假设我做错了。这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<flights
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test.xsd">

    <flight flightid="1">
        <plane planeid="1">Airbus A380-861</plane>
    </flight>

</flights>

这是XSD文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

    <xs:element name="flights">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="flight" 
                            type="flighttype" 
                            minOccurs='1' 
                            maxOccurs='unbounded' />
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="flightidisunique">
            <xs:selector xpath="flight"/>
            <xs:field xpath="@flightid"/>
        </xs:unique>
    </xs:element>

    <xs:complexType name="flighttype">
        <xs:sequence>
            <xs:element name="plane" 
                        type="planetype" 
                        minOccurs='1' 
                        maxOccurs='unbounded' />
        </xs:sequence>
        <xs:unique name="planeidisunique">
            <xs:selector xpath="plane"/>
            <xs:field xpath="@planeid"/>
        </xs:unique>
        <xs:attribute name="flightid" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="planetype">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="planeid" type="xs:string"/>
    </xs:complexType>

</xs:schema>

为我发布的XML创建有效架构的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

以下架构确保flightid在所有flight元素中都是唯一的planeid在同一plane元素中的所有flight元素中是唯一的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

  <xs:element name="flights">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flight" type="flighttype" minOccurs='1' maxOccurs='unbounded'>
          <xs:unique name="planeidisunique">
            <xs:selector xpath="plane"/>
            <xs:field xpath="@planeid"/>
          </xs:unique>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="flightidisunique">
      <xs:selector xpath="flight"/>
      <xs:field xpath="@flightid"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="flighttype">
    <xs:sequence>
      <xs:element name="plane" type="planetype" minOccurs='1' maxOccurs='unbounded' />
    </xs:sequence>
    <xs:attribute name="flightid" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="planetype" mixed="true">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="planeid" type="xs:string"/>
  </xs:complexType>

</xs:schema>

架构的这种变体使planeid在所有plane元素中都是唯一的:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

  <xs:element name="flights">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flight" type="flighttype" minOccurs='1' maxOccurs='unbounded'>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="flightidisunique">
      <xs:selector xpath="flight"/>
      <xs:field xpath="@flightid"/>
    </xs:unique>
    <xs:unique name="planeidisunique">
      <xs:selector xpath="flight/plane"/>
      <xs:field xpath="@planeid"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="flighttype">
    <xs:sequence>
      <xs:element name="plane" type="planetype" minOccurs='1' maxOccurs='unbounded' />
    </xs:sequence>
    <xs:attribute name="flightid" type="xs:string"/>
  </xs:complexType>

  <xs:complexType name="planetype" mixed="true">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="planeid" type="xs:string"/>
  </xs:complexType>

</xs:schema>

答案 1 :(得分:2)

想象你想要一个像这样的xml:

<flights xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">

    <flight flightid="f1">
        <plane planeid="f1p1">Airbus A380-861</plane>
        <plane planeid="f1p2">Airbus A380-861</plane>
        <plane planeid="f1p3">Airbus A380-861</plane>
        <plane planeid="f1p4">Airbus A380-861</plane>
    </flight>

    <flight flightid="f2">
        <plane planeid="f2p1">Airbus A380-862</plane>
    </flight>
</flights>

您需要这样的架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="flights">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="flight" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="flight">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="plane" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="flightid" use="required" type="xs:ID"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="plane">
    <xs:complexType mixed="true">
      <xs:attribute name="planeid" use="required" type="xs:ID"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

为了使您的ID具有唯一性,这些属性需要类型为“xs:ID”,而xs:ID是NCName,这意味着它的名称不是数字,例如:one或a1。 还将maxOccurs =“unbounded”添加到节点航班平面

希望这能回答你的问题。