I have an instance document in which I need to enforce that an attribute value is unique. Also, some other element of the document relates to this unique attribute value.
Here is an example XML instance document I made up to explain my problem
Code:
<?xml version="1.0" encoding="UTF-8"?>
<parts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<part ref="44" name="processor">
<description>Standard floppy</description>
<related-part>4</related-part>
</part>
<part ref="4" name="motherboard">
<description>Standard floppy</description>
<related-part>55</related-part>
</part>
</parts>
And here is the XML Schema document
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="parts">
<xs:complexType>
<xs:sequence>
<xs:element name="part" type="partType" minOccurs="2" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!-- Each part must have a unique "ref" attribute and "name" combination
Same part for different computer make could have the same name but needs
a different reference to track the part -->
<xs:unique name="partRef">
<xs:selector xpath="part"/>
<xs:field xpath="@ref"/>
</xs:unique>
<xs:unique name="partRefAndName">
<xs:selector xpath="item"/>
<xs:field xpath="@partNum"/>
<xs:field xpath="productName"/>
</xs:unique>
</xs:element>
<xs:complexType name="partType">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="related-part" maxOccurs="unbounded"/>
</xs:sequence>
<!-- Update to use attribute group
Remove whitespaces in names -->
<xs:attribute name="name" type="xs:token" use="required"/>
<xs:attribute name="ref" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:schema>
Now within the
parts context, I want to create a key to the
@ref attribute and ensure that the value of
related-parts refers to a existing
@ref
I have done this below but it does not work - no error messages
Code:
<xs:key name="key">
<xs:selector xpath="part"/>
<xs:field xpath="@ref"/>
</xs:key>
<xs:keyref name="refKey" refer="key">
<xs:selector xpath="part"/>
<xs:field xpath="related-parts"/>
</xs:keyref>
Please, let me know if you need more information
Thanks