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" />
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.