XML模式中属性值的唯一性
目前我有这个XML文件XML模式中属性值的唯一性,xml,xsd,Xml,Xsd,目前我有这个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="unq
<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:unique stament与更复杂的选择器合并在一起吗?因为如果我将添加另一个车辆(例如火车),我将强制编写非常相似的唯一块
我懒得写一些非常独特的块:)在您的示例中,有一辆@id=0的汽车,还有一个@id=0的飞机。这意味着有两组独立的唯一ID,一组用于汽车,一组用于飞机,因此必须有两个独立的唯一约束。如果所有车辆的id必须是唯一的,无论车辆类型如何,则您可以将约束合并为一个约束。是的,对于第二种情况,您是对的,我可以使用选择器。