Reputation: 1353
With some validators the following xsd causes some problems when validating xml files against it which seem to be valid. Dependent on the validator the error message looks something like this (libxml):
Schemas validity error : Element 'referringElement': No match found for key-sequence ['1'] of keyref 'reference'. Start location: 8:0
The error message is confusing because it seems that the referenced key (id=1) was defined.
This is the xsd which causes problems:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element ref="listOfReferencedElements"/>
<xs:element ref="referringElement"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="reference" refer="id">
<xs:selector xpath=".//*"/>
<xs:field xpath="@reference"/>
</xs:keyref>
</xs:element>
<xs:element name="listOfReferencedElements">
<xs:complexType>
<xs:sequence>
<xs:element name="referencedElement" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="id">
<xs:selector xpath="./referencedElement"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="referringElement">
<xs:complexType>
<xs:attribute name="reference" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
... and this a sample xml which leads to the error message above:
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/C:/Users/jakob/dev/projects/integration/trading-contracts/trunk/playground/Reference.xsd">
<listOfReferencedElements>
<referencedElement id="1"/>
<referencedElement id="2"/>
</listOfReferencedElements>
<referringElement reference="1"/>
</document>
Upvotes: 1
Views: 1804
Reputation: 1353
The problem seems to be that the type of the key is not compatible with the type of the reference. Amending the referring element in the following way (defining the type as xs:string) solves the problem:
<xs:element name="referringElement">
<xs:complexType>
<xs:attribute name="reference" type="xs:string"/>
</xs:complexType>
</xs:element>
Upvotes: 2