我想在 XML 模式中使用 key 和 keyref 在 xml 文档中实现引用完整性。
假设我的 XML 如下所示:
<root>
<parents>
<parent parentID="P01">XXX</parent>
<parent parentID="P02">XXX</parent>
</parents>
<sons>
<son sonID="S01" parentID="P01">XXX</son>
<son sonID="S02" parentID="P02">XXX</son>
<son sonID="S03" parentID="P02">XXX</son>
</sons>
</root>
例如,在我的 XML 中,我想将 parentID 属性定义为 parent 元素的主键。
我在 root 元素中声明了相应的 xs:key:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="root" type="myRootType" >
<xs:key name="parentKey">
<xs:selector xpath="./parents/parent" />
<xs:field xpath="@parentID"/>
</xs:key>
</xs:element>
<xs:complexType name="myRootType">
<xs:sequence>
....
在 xs:selector 的 xpath 属性中使用相对路径 ("./parents/parent) 元素,如上例所示。
但是如果我改用绝对路径(“/root/parents/parent”),身份限制就不起作用了。
这是为什么?我的理解是它应该以两种方式工作(在 XPath 表达式中使用绝对路径或相对路径。
最佳答案
实际上 XML 模式不支持 <xs:selector>
中的任何 XPath 表达式,而是一个不包含以 /
开头的表达式的子集 .即正式描述here .
除了不工作之外,您的处理器应该从 /root/parents/parent
开始产生错误消息是一个无效的表达式。
关于XML Schema Identity constraints - 我可以在选择器的 XPath 中使用绝对路径吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22347949/