timone5666
New Member
I have a requirement to enforce uniqueness of an attribute of an element, but only within the scope of the parent element. Here is an example of valid XML\[code\]<ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:Test.Namespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" > <ns:element1 id="001"> <ns:element2 id="001.1" order="1"> <ns:element3 id="001.1.1" /> </ns:element2> <ns:element2 id="001.2" order="2"> <ns:element3 id="001.1.2" /> </ns:element2> </ns:element1> <ns:element1 id="002"> <ns:element2 id="002.1" order="1"> <ns:element3 id="002.1.1" /> </ns:element2> <ns:element2 id="002.2" order="2"> <ns:element3 id="002.1.2" /> </ns:element2> </ns:element1> </ns:Root>\[/code\]Note above, that there are two sets of 'element1' and within that the element2 nodes have an attribute called 'order'. The requirement is that 'order' must be unique within the parent 'element1'. For example, this simplified version would be invalid XML\[code\]<ns:Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns="urn:Test.Namespace" xsi:schemaLocation="urn:Test.Namespace Test1.xsd" > <ns:element1 id="001"> <ns:element2 id="001.1" order="1"> <ns:element3 id="001.1.1" /> </ns:element2> <ns:element2 id="001.2" order="1"> <ns:element3 id="001.1.2" /> </ns:element2> </ns:element1> </ns:Root>\[/code\]I have written the following Schema to do this for me;\[code\]<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Test.Namespace" xmlns:ns="urn:Test.Namespace" elementFormDefault="qualified"> <xsd:element name="Root"> <xsd:complexType> <xsd:sequence> <xsd:element name="element1" maxOccurs="unbounded" type="ns:element1Type"/> </xsd:sequence> </xsd:complexType> <xsd:unique name="uniqueElement2OrderInElement1"> <xsd:selector xpath="./ns:element1" /> <xsd:field xpath="ns:element2/@order" /> </xsd:unique> </xsd:element> <xsd:complexType name="element1Type"> <xsd:sequence> <xsd:element name="element2" maxOccurs="unbounded" type="ns:element2Type"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="element2Type"> <xsd:sequence> <xsd:element name="element3" type="ns:element3Type" /> </xsd:sequence> <xsd:attribute name="id" type="xsd:string" /> <xsd:attribute name="order" type="xsd:string" /> </xsd:complexType> <xsd:complexType name="element3Type"> <xsd:attribute name="id" type="xsd:string"/> </xsd:complexType> </xsd:schema>\[/code\]This is close, in that it does enforce uniqueness, but document wide. ie every order attribute has to be unique. I think this is because it is placed in the schema for Root, but I have tried to move it to a more localised place, or make the selector more specific and it does not work (I get errors).Is what I am trying to do possible?Many thanks in anticipation