如何控制XML中元素之间的引用

时间:2015-04-01 12:16:13

标签: xml reference xsd

在我之前的问题(How create references between elements in XML)中,我问过如何在XML Schema中创建元素之间的引用。

好的,但现在我发现了一些东西。例如,定义这三种类型的车辆:

<Car id="car1">
    <plate>AAA</plate>
    <mark>Peugeot</mark>
    <model>206</model>
</Car>

<Truck id="truck1">
    <plate>BBB</plate>
    <mark>Scania</mark>
    <model>X1</model>
</Truck>

<Trailer id="trailer1">
    <plate>CCC</plate>
    <mark>Scania</mark>
    <model>T1</model>
</Trailer>

...我现在可以定义复杂车辆,例如:

<TrailerTruck id="tt1">
    <Car refid="car1"/>
    <Trailer refid="trailer1"/>
</TrailerTruck>

<TrailerTruck id="tt2">
    <Truck refid="truck1"/>
    <Trailer refid="trailer1"/>
</TrailerTruck>

好的,它有效,但我注意到 - 不幸的是 - 也允许这样的事情:

<TrailerTruck id="tt3_WRONG">
    <Truck refid="trailer1"/>   <!-- an element "Truck" should not
    refer to a trailer!! Should be able to refer only to truck1 -->
    <Trailer refid="car1"/>     <!-- like before, an element Trailer
    should be able to refer only trailer1 -->
</TrailerTruck>

<TrailerTruck id="tt4_WRONG">
    <Car refid="truck1"/>       <!-- an element **Car** should not
    refer to a Truck !! -->
    <Trailer refid="trailer1"/>
</TrailerTruck>

因此,如果我输入错误的引用,则不会显示任何错误。相反,我希望得到通知。我想插入某种限制或控制。但是如何?


现在我向您展示我目前的实施情况。

VehicleXMLSchema.xsd

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

    <xs:element name="Fleet" type="FleetType"/>


    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element ref="Car" />
            <xs:element ref="Truck" />
            <xs:element ref="Trailer" />
            <xs:element ref="TrailerTruck" />
        </xs:choice>
    </xs:complexType>


    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence minOccurs="0">
            <xs:element name="plate" type="xs:string" minOccurs="1" />
            <xs:element name="mark" type="xs:string" minOccurs="1" />
            <xs:element name="model" type="xs:string" minOccurs="1" />
        </xs:sequence>
    </xs:complexType>




    <!-- SimpleVehicle and ComplexVehicle -->

    <xs:complexType name="SimpleVehicle">
        <xs:complexContent>
            <xs:extension base="VehicleType">
                <xs:attribute name="id" type="xs:ID"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="ComplexVehicle">
        <xs:complexContent>
            <xs:extension base="VehicleType">
                <xs:attribute name="refid" type="xs:IDREF"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>




    <!-- Car, Truck, Trailer -->

    <xs:element name="Car" type="SimpleVehicle" />
    <xs:element name="Truck" type="SimpleVehicle"/>
    <xs:element name="Trailer" type="SimpleVehicle"/>




    <!-- TrailerTruck -->

    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element name="Car" type="ComplexVehicle" />
            <xs:element name="Van" type="ComplexVehicle"/>
            <xs:element name="Truck" type="ComplexVehicle"/>
        </xs:choice>
    </xs:group>

    <xs:element name="TrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:group ref="DrivingPart" minOccurs="1" maxOccurs="1"/>
                <xs:element name="Trailer" type="ComplexVehicle" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

Shipper1.xml

<Fleet shipperName="Shipper1" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="VehicleXMLSchema.xsd">

    <Car id="car1">
        <plate>AAA</plate>
        <mark>Peugeot</mark>
        <model>206</model>
    </Car>

    <Truck id="truck1">
        <plate>DDD</plate>
        <mark>Scania</mark>
        <model></model>
    </Truck>

    <Trailer id="trailer1">
        <plate>EEE</plate>
        <mark>Scania</mark>
        <model></model>
    </Trailer>


    <TrailerTruck id="trailerTruck1">
        <Car refid="car1" />
        <Trailer refid="trailer1" />
    </TrailerTruck>

    <TrailerTruck id="trailerTruck2_WRONG">
        <Truck refid="car1" />
        <Trailer refid="trailer1" />
    </TrailerTruck>

    <TrailerTruck id="trailerTruck3_WRONG">
        <Truck refid="truck1" />
        <Trailer refid="car1" />
    </TrailerTruck>

</Fleet>

1 个答案:

答案 0 :(得分:9)

我认为你正在寻找参照完整性;你真的需要xsd:key/xsd:keyref。 ID / IDREF在XSD规范中,以便与DTD向后兼容;我不会在XSD中使用它们。

这是您修改过的XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Fleet" type="FleetType">
        <xs:key name="PK_Cars">
            <xs:selector xpath="Car"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:key name="PK_Trucks">
            <xs:selector xpath="Truck"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:key name="PK_Trailers">
            <xs:selector xpath="Trailer"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:key name="PK_TrailerTruck">
            <xs:selector xpath="TrailerTruck"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:keyref name="FK_TrailerTruck2Trailers" refer="PK_Trailers">
            <xs:selector xpath="TrailerTruck/Trailer"/>
            <xs:field xpath="@refid"/>
        </xs:keyref>
        <xs:keyref name="FK_TrailerTruck2Cars" refer="PK_Cars">
            <xs:selector xpath="TrailerTruck/Car"/>
            <xs:field xpath="@refid"/>
        </xs:keyref>
        <xs:keyref name="FK_TrailerTruck2Trucks" refer="PK_Trucks">
            <xs:selector xpath="TrailerTruck/Truck"/>
            <xs:field xpath="@refid"/>
        </xs:keyref>
    </xs:element>
    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element ref="Car"/>
            <xs:element ref="Truck"/>
            <xs:element ref="Trailer"/>
            <xs:element ref="TrailerTruck"/>
        </xs:choice>
        <xs:attribute name="shipperName" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence minOccurs="0">
            <xs:element name="plate" type="xs:string" minOccurs="1"/>
            <xs:element name="mark" type="xs:string" minOccurs="1"/>
            <xs:element name="model" type="xs:string" minOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
    <!-- SimpleVehicle and ComplexVehicle -->
    <xs:complexType name="SimpleVehicle">
        <xs:complexContent>
            <xs:extension base="VehicleType">
                <xs:attribute name="id" type="xs:ID"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="ComplexVehicle">
        <xs:complexContent>
            <xs:extension base="VehicleType">
                <xs:attribute name="refid" type="xs:IDREF"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <!-- Car, Truck, Trailer -->
    <xs:element name="Car" type="SimpleVehicle"/>
    <xs:element name="Truck" type="SimpleVehicle"/>
    <xs:element name="Trailer" type="SimpleVehicle"/>
    <!-- TrailerTruck -->
    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element name="Car" type="ComplexVehicle"/>
<!--            <xs:element name="Van" type="ComplexVehicle"/>-->
            <xs:element name="Truck" type="ComplexVehicle"/>
        </xs:choice>
    </xs:group>
    <xs:element name="TrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:group ref="DrivingPart" minOccurs="1" maxOccurs="1"/>
                <xs:element name="Trailer" type="ComplexVehicle" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

使用key / keyref,您现在可以控制交叉检查。下图仅显示了展示的内容:

  • 汽车和拖车PK
  • 如何将拖车与拖车挂钩
  • 如何将您的驾驶部件(本例中为汽车)与相关的对应部件(XML顶部的汽车)相关联。

enter image description here

它与“传统”数据库设计中的引用完整性一致:您获得了主键,外键。您发布的XML现在无效;下面的那个将是它的有效版本。

<Fleet shipperName="Shipper1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="VehicleXMLSchema.xsd">

    <Car id="car1">
        <plate>AAA</plate>
        <mark>Peugeot</mark>
        <model>206</model>
    </Car>

    <Truck id="truck1">
        <plate>DDD</plate>
        <mark>Scania</mark>
        <model></model>
    </Truck>

    <Trailer id="trailer1">
        <plate>EEE</plate>
        <mark>Scania</mark>
        <model></model>
    </Trailer>

    <TrailerTruck id="trailerTruck1">
        <Car refid="car1"/>
        <Trailer refid="trailer1"/>
    </TrailerTruck>

    <TrailerTruck id="trailerTruck2_WRONG">
        <Truck refid="truck1"/>
        <Trailer refid="trailer1"/>
    </TrailerTruck>

    <TrailerTruck id="trailerTruck3_WRONG">
        <Truck refid="truck1"/>
        <Trailer refid="trailer1"/>
    </TrailerTruck>

</Fleet>