我正在尝试通过使用 XSD<xs:key>
和<xs:keyref>
元素以我的自定义 XML 格式强制执行键和引用约束。它没有像我希望的那样工作。
首先是我的 XML 格式的示例:
<room xmlns="http://example.com">
<box>
<item name="x" uses="y" />
<item name="y" uses="z" />
<item name="z" />
<box>
<item name="p" uses="q" />
<item name="q" uses="r" />
<item name="r" />
<box>
</box>
</box>
</box>
</room>
这个数据结构描述了一个“房间”,里面有一个“盒子”。一个盒子可以包含项目和其他盒子。一个盒子也可以是空的。盒子中的物品必须具有不同的名称(但可以与其他盒子中的物品共享名称),并且只能“使用”同一盒子中的其他物品。
我试图通过在适当的属性上使用 key/keyrefs 来保持“使用”图的完整性。但是,当我使用下面的模式在此 XML 上使用 Xerces 2 验证器时,我收到以下错误:
[Error] file:///example.xml:13:11: cvc-identity-constraint.4.3: Key 'ItemKeyRef' with value 'q' not found for identity constraint of element 'box'.
[Error] file:///example.xml:14:9: cvc-identity-constraint.4.3: Key 'ItemKeyRef' with value 'y' not found for identity constraint of element 'box'.
架构:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:example="http://example.com"
targetNamespace="http://example.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="room" type="example:Room" />
<xs:complexType name="Room">
<xs:all>
<xs:element ref="example:box" />
</xs:all>
</xs:complexType>
<xs:element name="box" type="example:Box">
<xs:keyref name="ItemKeyRef" refer="example:ItemKey">
<xs:selector xpath="./example:item" />
<xs:field xpath="@uses" />
</xs:keyref>
<xs:key name="ItemKey">
<xs:selector xpath="./example:item" />
<xs:field xpath="@name" />
</xs:key>
</xs:element>
<xs:complexType name="Box">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="item" type="example:Item" />
<xs:element ref="example:box" />
</xs:choice>
</xs:complexType>
<xs:complexType name="Item">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="uses" type="xs:string" />
</xs:complexType>
</xs:schema>
我觉得我做错了什么。为什么我会收到此错误?这不应该验证吗?