是否可以引用xml文件中的现有元素? 我在谷歌搜索但没有找到我希望的答案。
我立即开始解释我的目标:
<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 。 VehiclesType 是抽象的。而 Car , Truck 等具体而言。 注意 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>
答案 0 :(得分:3)
您可以简单地使用架构的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>
评论:
<TrailerTruck>
内的引用。<TruckRef>
,<TrailerRef>
。因此,您需要定义这些标记并修改<TrailerTruck>
。答案 1 :(得分:0)
您可以使用XPointer来引用XML的其他部分。