1

我无法弄清楚如何创建我的 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 创建有效架构的正确方法是什么?

4

2 回答 2

3

以下模式确保flightid在所有flight元素中是唯一的,并且在同一元素内的planeid所有元素中是唯一的:planeflight

<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>
于 2013-02-12T00:54:31.810 回答
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。还向节点flightplane添加了 maxOccurs="unbounded"

希望这能回答你的问题。

于 2013-02-12T00:35:08.497 回答