目前我有这个XML文件。
<Vehicles>
<Car id="0" name="Kia" />
<Car id="1" name="Opel"/>
<Plane id="0" name="Airbus"/>
<Plane id="1" name="Boeing" />
</Vehicles>
以及它的模式:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="VehicleType">
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
<xs:element name="Vehicles">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Car" type="VehicleType" />
<xs:element name="Plane" type="VehicleType" />
</xs:choice>
</xs:complexType>
<xs:unique name="CarIdsMustBeUnique">
<xs:selector xpath="Car" />
<xs:field xpath="@id"/>
</xs:unique>
<xs:unique name="PlaneIdsMustBeUnique">
<xs:selector xpath="Plane" />
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
正如你所看到的,我希望汽车和飞机的id是独一无二的,但我必须写两个独特的块来实现它。我能否尽量减少代码重复并加入xs:在一个更复杂的选择器中具有独特的特性?因为如果我将增加另一辆车(例如火车),我会强迫写非常简单的独特的块。
我懒得写一些非常独特的块:)
发布于 2018-04-10 09:32:08
在您的示例中,有一辆带有@id=0的汽车,也有一架带有@id=0的飞机。这意味着有两组独立的唯一ID,一组用于汽车,另一组用于飞机,因此必须有两个单独的唯一约束。如果所有车辆的id必须是唯一的,无论车辆类型如何,那么您将能够将这些约束组合成一个。
https://stackoverflow.com/questions/49757524
复制相似问题