I
think this is a mistake in the Schema, but a very easy one to make since,
unfortunately, this is a VERY non-intuitive part of the spec (I have seen this
situation a few times and it still seems counter-intuitive to me).
What basically happens is that the default
prefix declaration *is not* taken into account for the XPath
expressions describing the keys. (The reason for it is that the XPath
expressions appear as attribute text, and arbitrary text and active default
prefix declaration don't mix well). Of course, you *can* describe keys that
point to elements in the default namespace and you may want to do just that, so
the situation is not detectable at compilation time.
And
during validation, the only rule for key is that each "instance" of the
"selector" has a corresponding instance of the "field" (and that the "field"
instances are of course unique), but if there are no instances of the "selector"
element, then it's ok, no error.
To
make a long story short, in your original example, you need to declare a
specific prefix for the "test" namespace and use that in you XPath expressions
(or use elementFormDefault="unqualified" since this also interacts with the
selectors).
Thanks,
Radu
-----Original Message-----
From: Olivier CHAPITEAU [mailto:[EMAIL PROTECTED]
Sent: Friday, January 13, 2006 1:02 AM
To: [email protected]
Subject: key, keyref with namespace : not working
From: Olivier CHAPITEAU [mailto:[EMAIL PROTECTED]
Sent: Friday, January 13, 2006 1:02 AM
To: [email protected]
Subject: key, keyref with namespace : not working
If i declare a target namespace in a schema using key and keyref, constraint isn't verified by validate (alwasy tell valid)
did i do a mistake in my schemas or is it a bug ?
thanks,
the details of schema and sample xml :
The keyref.xsd with a target namespace declared :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" xmlns="test"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="List" type="ListType"/>
<xs:element name="Refs" type="RefType"/>
</xs:sequence>
</xs:complexType>
<xs:key name="myId">
<xs:selector xpath=".//List/a"/>
<xs:field xpath="./@id"/>
</xs:key>
<xs:keyref refer="myId" name="myIdRef">
<xs:selector xpath=".//Refs/b"/>
<xs:field xpath="./@idref"/>
</xs:keyref>
</xs:element>
<xs:complexType name="ListType">
<xs:sequence>
<xs:element name="a" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:NCName" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RefType">
<xs:sequence>
<xs:element name="b" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="idref" type="xs:NCName" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
here's a nonvalid.xml document :
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="test" >
<List>
<a id="x"/>
<a id="y"/>
</List>
<Refs>
<b idref="x"/>
<b idref="y"/>
<b idref="z"/>
</Refs>
</root>
if I do "validate keyref.xsd nonvalid.xml" , it tell me that document is valid.
Removing any targetnamespace declaration, the key is correctly checked :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" xmlns="test"
elementFormDefault="qualified"> become <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<root xmlns="test" > become <root> in xml doc
then, the validation fail :"key ref "z" not found", as awaited
Olivier,

