gpt4 book ai didi

xml - 如何在 XML 中的元素之间创建引用

转载 作者:行者123 更新时间:2023-12-04 02:21:08 25 4
gpt4 key购买 nike

是否可以引用 xml 文件中的现有元素?我在 Google 上进行了搜索,但没有找到我希望得到的答案。

我立即开始解释我的目标:

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

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

<Trailer id="trailer1">
<plate>CCC</plate>
<mark>Menci</mark>
</Trailer>

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

...这样我就不会复制任何东西。

可以做这种引用吗?如果是这样,如何


我向您展示我的XML 架构:

<?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 name="Car" type="CarType"/>
<xs:element name="Truck" type="TruckType"/>
<xs:element name="Trailer" type="TrailerType"/>
<xs:element name="TrailerTruck" type="TrailerTruckType"/>
</xs:choice>
</xs:complexType>


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



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

<xs:complexType name="CarType">
<xs:complexContent>
<xs:extension base="VehicleType"/>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="TruckType">
<xs:complexContent>
<xs:extension base="VehicleType"/>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="TrailerType">
<xs:complexContent>
<xs:extension base="VehicleType"/>
</xs:complexContent>
</xs:complexType>



<!-- Definition: TrailerTruck -->

<xs:group name="DrivingPart">
<xs:choice>
<xs:element name="Car" type="CarType"/>
<xs:element name="Truck" type="TruckType"/>
</xs:choice>
</xs:group>

<xs:complexType name="TrailerTruckType">
<xs:sequence>
<xs:group ref="DrivingPart"/>
<xs:element name="Trailer" type="TrailerType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

在这个 XSD 中,我有一个 Fleet VehiclesVehiclesType 是抽象的。相反,CarTruck 等是具体的。关注 TrailerTruck

一个XML的例子:

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

<Car>
<plate>AAA</plate>
<mark>Peugeot</mark>
<model>206</model>
</Car>

<Truck>
<plate>BBB</plate>
<mark>Scania</mark>
<model>X1</model>
</Truck>

<Trailer>
<plate>CCC</plate>
<mark>Menci</mark>
<model>m2</model>
</Trailer>


<TrailerTruck>
<Truck> <!-- Here I'm OBBLIGATE to rewrite all :( --->
<plate>BBB</plate>
<mark>Scania</mark>
<model></model>
</Truck>
<Trailer>
<plate>CCC</plate>
<mark>Menci</mark>
<model>m2</model>
</Trailer>
</TrailerTruck>


</Fleet>

最佳答案

您可以简单地使用架构的 ID/IDREF 功能。但是,您需要一个引用 ID 值的特定属性。

因此我建议您像这样修改架构:

<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="0" />
<xs:element name="model" type="xs:string" minOccurs="1" />
</xs:sequence>
<xs:attribute name="id" type="xs:ID"></xs:attribute>
<xs:attribute name="refid" type="xs:IDREF"></xs:attribute>
</xs:complexType>

连同 XML 实例,例如:

<?xml version="1.0" encoding="UTF-8"?>
<Fleet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd">
<Car id="car1">
<plate>AAA</plate>
<mark>Peugeot</mark>
<model>Model AAA</model>
</Car>

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

<Trailer id="trailer1">
<plate>CCC</plate>
<mark>Menci</mark>
<model>Model CCC</model>
</Trailer>

<TrailerTruck>
<Truck refid="truck1"/>
<Trailer refid="trailer1" />
</TrailerTruck>
</Fleet>

评论:

  1. 现在卡车或拖车的内容可以是空的。是否要使用完整内容由您自己决定(仅供引用)。这最大限度地减少了模式的修改。
  2. 如果您确定只需要 <TrailerTruck> 中的引用,则可以在只允许空内容的 TrailerTruck 中本地定义 Truck/Trailer 元素。 .
  3. 但是(恕我直言)我认为最好使用特定的标签作为引用,例如<TruckRef> , <TrailerRef> .因此您需要定义这些标签并修改 <TrailerTruck> 的模型内容.

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

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