I am trying to better understand key and keyref and how to use them in a schema. I would like to apply a key to parts of type y, but not parts of type z. I am confused as to why I am seeing the following error.
'r:B/r:part[@type='y']' is an invalid XPath for selector or field
I am all but certain that the XPath is valid, but I get an error from Visual Studio as soon as I enter the predicate filter in the XPath expression.
Here is the XML
<root xmlns="namespace1">
<A>
<!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
<part ref-number="1"/>
</A>
<A>
<!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
<part ref-number="2"/>
</A>
<B>
<part type="y" key-number="1"/>
<part type="y" key-number="2"/>
<part type="z" key-number="3"/>
</B>
</root>
Here is the schema
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="namespace1" xmlns:r="namespace1" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="A" type="r:aType" maxOccurs="unbounded">
<xs:keyref name="dummy" refer="r:partNumberKey">
<xs:selector xpath="r:part"/>
<xs:field xpath="@ref-number"/>
</xs:keyref>
</xs:element>
<xs:element name="B" type="r:bType"/>
</xs:sequence>
</xs:complexType>
<xs:key name="partNumberKey">
<!-- I get an error here -->
<xs:selector xpath="r:B/r:part[@type='y']"/>
<xs:field xpath="@key-number"/>
</xs:key>
</xs:element>
<xs:complexType name="aType">
<xs:sequence>
<xs:element name="part" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ref-number" type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bType">
<xs:sequence>
<xs:element name="part" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key-number" type="xs:integer"/>
<xs:attribute name="type" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
/root/B/part[@type='y'][2]/@type- Royi Namir