Reputation: 2155
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.
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>
<globalElement>
<subnode value="1" />
<subnode value="2" />
</globalElement>
<globalElement>
<subnode value="1" /> <!-- Desired error here -->
<subnode value="1" /> <!-- Desired error here -->
</globalElement>
<globalElement>
<subnode value="1" /> <!-- Error here -->
<subnode value="2" />
</globalElement>
<globalElement>
<subnode value="1" /> <!-- Error here -->
<subnode value="1" /> <!-- Error here -->
</globalElement>
<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>
Upvotes: 0
Views: 768
Reputation: 163322
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.
Upvotes: 0
Reputation: 3428
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>
Upvotes: 1