Joe W
Joe W

Reputation: 1890

xs:unique - Enforce Unique Attribute Values in Schema on abstract element

I'm trying to force uniqueness of an attribute using xs:unique. However, I think I'm having some trouble with XPath.

I have an abstract element, ObjectA, that has an attribute "identifier" that is a string. I want this identifier to be unique across all instances of the concrete elements ObjectB and ObjectC. I don't think xpath="." is correct.

<xs:element name="ObjectA" type="ns:ObjectAType">
  <xs:unique name="Identifier">
    <xs:selector xpath="."/>
    <xs:field xpath="@identifier"/>
  </xs:unique>
</xs:element>

<xs:complexType name="ObjectAType" abstract="true">
  <xs:attribute ref="ns:identifier" use="required" />
</xs:complexType>

<xs:attribute name="identifier" type="xs:string" />

<xs:element name="ObjectB" type="ns:ObjectBType" substitutionGroup=ns:ObjectA" />
<xs:element name="ObjectC" type="ns:ObjectCType" substitutionGroup=ns:ObjectA" />

Upvotes: 2

Views: 767

Answers (2)

Michael Kay
Michael Kay

Reputation: 163342

If you want every A element within a containing D element to have a unique value for F, then you should define the unique constraint at the level of the D element; the selector should select the A elements starting from the D, and the field should select the value of F starting from the A element. Your mistake is you're trying to define the constraint on the A element, which is wrong, because no individual A element will be invalid; the invalidity is at the level of D.

Hope this helps.

Upvotes: 2

Baski
Baski

Reputation: 839

Unique constraint works more on collection than the individual element. The schema definition you have is more or less valid except that unique is checked "within" element of Objectx rather across the object.If you post the complete schema it will help to give more accurate definition.

Upvotes: 0

Related Questions