I can't seem to get XPATH for XSD UNIQUE Constraint

I tried to set a UNIQUE restriction on some attributes that I have in a fairly simple XSD schema. I am using XMLSpy and I want to set a unique constraint around TEMPLATE.ID for my nodes (i.e. it is important that they remain unique).

I put below in place and tried the following:

Secenario 1

 <xs:unique name="uniqueviewid"> <xs:selector xpath="./views"/> <xs:field xpath="@id"/> </xs:unique> 

Result: XSD: checks OK. XML Validates but does not support a unique constraint (i.e. two or more elements with the same identifier are not selected

Scenario 2

 <xs:unique name="uniqueviewid"> <xs:selector xpath="views"/> <xs:field xpath="@id"/> </xs:unique> 

** Results * Same as scenario 1. XSD Validates, XML validates but ignores duplicate @id in view element

Scenario 3

 <xs:unique name="uniqueviewid"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> 

Results: XSD Validates and XML Validats, and it respects the UNIQUE constrint (i.e. if duplicated views using @id it produces an invaliddation according to the design).

The problem is that the wild card is "*", which means that all trays for VIEWPODS will be checked, which is not what I want. Instead, I want the use case to focus on the exact path of VIEWPOS / VIEWS / @ ID.

I think my XPATH is wrong, but I can’t come up with what I am doing specifically wrong?


XML example.

This is an example of XML.

 <config xmlns="http://tempuri.org/RIAGenicConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tempuri.org/RIAGenicConfig.xsd RIAGenicConfig.xsd"> <view> <viewpods> <views id="view1"/> <views id="view1"/> </viewpods> </view> </config> 

XSD in question.

 <xs:schema xmlns="http://tempuri.org/RIAGenicConfig.xsd" xmlns:mstns="http://tempuri.org/RIAGenicConfig.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tempuri.org/RIAGenicConfig.xsd" elementFormDefault="qualified" id="RIAGenicConfig"> <xs:element name="config"> <xs:complexType> <xs:sequence> <xs:element name="view"> <xs:complexType> <xs:choice> <xs:element name="viewpods"> <xs:complexType> <xs:choice> <xs:element name="views" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="id"/> </xs:complexType> </xs:element> <xs:element name="blah"/> </xs:choice> </xs:complexType> <xs:unique name="uniqueviewid"> <xs:selector xpath="*"/> <xs:field xpath="@id"/> </xs:unique> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 
+4
source share
1 answer

Note the xpath choice for mstns: views instead of * (all elements):

 <xs:unique name="uniqueviewid"> <xs:selector xpath="mstns:views"/> <xs:field xpath="@id"/> </xs:unique> 
+3
source

All Articles