gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:44:57 24 4
gpt4 key购买 nike

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

好的,但现在我注意到了一些事情。例如,定义这三种类型的车辆:

<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>

最佳答案

我认为您正在寻找参照完整性;你真的需要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>

关于xml - 如何控制 XML 中元素之间的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29390593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com