Question

Objective:

For a schema I am creating, I would like to have a uniqueness constraint that checks for uniqueness only within each parent node, as opposed to checking the entire schema for instances of the subnode. I think this is best illustrated by example, see below xml.

Problem:

The xpath I'm using checks for uniqueness against all instances of <subnode value="x">, whereas I want it to check uniqueness against only instances of <subnode value="x"> that are children of, and here's the clencher, separate instances of <globalElement>

XML with Desired Error:

<globalElement>
    <subnode value="1" />
    <subnode value="2" />
</globalElement>

<globalElement>
    <subnode value="1" />            <!-- Desired error here -->
    <subnode value="1" />            <!-- Desired error here -->
</globalElement>

XML with Current Error:

<globalElement>
    <subnode value="1" />           <!-- Error here -->
    <subnode value="2" />
</globalElement>

<globalElement>
    <subnode value="1" />            <!-- Error here -->
    <subnode value="1" />            <!-- Error here -->
</globalElement>

XML Schema (1.0):

<xs:element name="rootElement">
    <xs:complexType>
         <xs:choice minOccurs="0" maxOccurs="unbounded" >
             <xs:any namespace="##targetNamespace"  />
         </xs:choice>
    </xs:complexType>       
     <xs:unique name="name_check">
          <xs:selector xpath=".//prefix:globalElement/prefix:subnode" />
          <xs:field xpath="@value" />
     </xs:unique>
</element>
Was it helpful?

Solution 2

The unique constraint in schema is quite simple if you keep it simple. If you want every B within an A to have a unique value for C (e.g. every person within a country to have a unique value for passport number), then you should define the constraint at the level of element A, the selector should select B relative to A, and the field should select C relative to B.

OTHER TIPS

I think you should define unique constraint on that parent element - in this case on globalElement

<xs:element name="GlobalElement">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="SubNode" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="value" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="name_check">
        <xs:selector xpath="SubNode"/>
        <xs:field xpath="@value"/>
    </xs:unique>
</xs:element>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top