xsd 中的 keyref 不工作
keyref in xsd not working
下面是我的 xsd 和 xml 的简化版本。
我正在尝试检查每个 OrderedPart@rPartId 在 Part@PartId 中是否具有有效匹配项。
我试过的所有工具都告诉我这个 XML 对这个 xsd 是有效的。
但是二阶应该给出错误,因为 67 不是有效的 Part@PartId。
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://localhost" xmlns="http://localhost" targetNamespace="http://localhost" elementFormDefault="qualified" attributeFormDefault="unqualified" >
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Order" type="OrderType" maxOccurs="unbounded"/>
<xs:element name="Parts" type="PartType"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="dummy" refer="pNumKey">
<xs:selector xpath="tns:OrderedPart" />
<xs:field xpath="@rPartId"/>
</xs:keyref>
<xs:key name="pNumKey">
<xs:selector xpath="tns:Part"/>
<xs:field xpath="@PartId"/>
</xs:key>
</xs:element>
<xs:complexType name="OrderType">
<xs:sequence>
<xs:element name="OrderedPart" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="rPartId" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PartType">
<xs:sequence>
<xs:element name="Part" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="PartId" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
和XML:
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KeyRefs.xsd" xmlns="http://localhost" >
<Order>
<OrderedPart rPartId="1"/>
</Order>
<Order>
<OrderedPart rPartId="67"/> <!-- validation should give error -->
</Order>
<Parts>
<Part PartId="1"/>
</Parts>
</root>
怀疑它与选择器中的 xPaths and/or 命名空间有关。
根据本网站其他帖子的输入,我已经尝试过命名空间组合,但无法正常工作。
欢迎任何建议。
(作为 OP 的更新似乎丢失了一些 XSD)
您 XSD 的主要问题是您没有正确使用 select 或。 选择器是相对于它们所属的元素的 正如XSD specs:
中所说
{selector} specifies a restricted XPath ([XPath]) expression relative
to instances of the element being declared.
因此,您需要将 xpath select 更改为 tns:Order/tns:OrderedPart
和 tns:Parts/tns:Part
,以便 select 正确的元素。
此外,您正在使用 PartId
属性作为键,但它被定义为可选属性。通常这不是期望的行为,因为如果密钥不存在则会出错,因此您可以在属性中使用 use="required"
。
下面是我的 xsd 和 xml 的简化版本。 我正在尝试检查每个 OrderedPart@rPartId 在 Part@PartId 中是否具有有效匹配项。
我试过的所有工具都告诉我这个 XML 对这个 xsd 是有效的。 但是二阶应该给出错误,因为 67 不是有效的 Part@PartId。
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://localhost" xmlns="http://localhost" targetNamespace="http://localhost" elementFormDefault="qualified" attributeFormDefault="unqualified" >
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Order" type="OrderType" maxOccurs="unbounded"/>
<xs:element name="Parts" type="PartType"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="dummy" refer="pNumKey">
<xs:selector xpath="tns:OrderedPart" />
<xs:field xpath="@rPartId"/>
</xs:keyref>
<xs:key name="pNumKey">
<xs:selector xpath="tns:Part"/>
<xs:field xpath="@PartId"/>
</xs:key>
</xs:element>
<xs:complexType name="OrderType">
<xs:sequence>
<xs:element name="OrderedPart" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="rPartId" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PartType">
<xs:sequence>
<xs:element name="Part" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="PartId" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
和XML:
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KeyRefs.xsd" xmlns="http://localhost" >
<Order>
<OrderedPart rPartId="1"/>
</Order>
<Order>
<OrderedPart rPartId="67"/> <!-- validation should give error -->
</Order>
<Parts>
<Part PartId="1"/>
</Parts>
</root>
怀疑它与选择器中的 xPaths and/or 命名空间有关。 根据本网站其他帖子的输入,我已经尝试过命名空间组合,但无法正常工作。
欢迎任何建议。
(作为 OP 的更新似乎丢失了一些 XSD)
您 XSD 的主要问题是您没有正确使用 select 或。 选择器是相对于它们所属的元素的 正如XSD specs:
中所说{selector} specifies a restricted XPath ([XPath]) expression relative to instances of the element being declared.
因此,您需要将 xpath select 更改为 tns:Order/tns:OrderedPart
和 tns:Parts/tns:Part
,以便 select 正确的元素。
此外,您正在使用 PartId
属性作为键,但它被定义为可选属性。通常这不是期望的行为,因为如果密钥不存在则会出错,因此您可以在属性中使用 use="required"
。