I want to implement referential integrity in a xml document using key & keyref in a XML Schema.
Let's say my XML looks like this:
<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>
In my XML, for example, I want to define the parentID attribute as a primary key for the parent element.
I declare the corresponding xs:key inside the root element:
<?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>
....
It works fine (validation gives an error if two parents have the same ID) using a relative path ("./parents/parent) in the xpath attribute of the xs:selector element, as in the example shown above.
But the identity restriction doesn't work if I use the absolute path instead ("/root/parents/parent").
Why is this? my understanding whas that it should work in both ways (using abosolute or relative paths in the XPath expression.
Actually XML Schema doesn't support any XPath expression in <xs:selector>
, but a subset which doesn't include expressions that start with /
. That is formally described here.
Besides not working, your processor should have produced an error message since /root/parents/parent
is an invalid expression.