How can I specify a unique constraint for attributes using <xs: unique> as a child of the <xs: element> tag?

If I have the following item specification in XSD, I can add the <xs:unique> constraint as a child of <xs:element name="Parent"> , but I cannot make it work as a child of <xs:element name="Child"> :

 <xs:element name="Parent"> <xs:complexType> <xs:element name="Child" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="Key" type="xs:string" use="required" /> </xs:complexType> <!-- Option A: insert unique constraint here with selector . --> </xs:element> </xs:complexType> <!-- Option B: insert unique constraint here with selector ./Child --> </xs:element> 

This is the only restriction that works as a child of <xs:element name="Parent"> :

 <xs:unique name="ChildKey"> <xs:selector xpath="./Child"/> <xs:field xpath="@Key" /> </xs:unique> 

But this unique restriction does not work as a child of <xs:element name="Child"> :

 <xs:unique name="ChildKey"> <xs:selector xpath="."/> <xs:field xpath="@Key" /> </xs:unique> 

Do I need to change the XPath selector in the second case?

+4
source share
1 answer

If you think about it, the selector is "." will always return the current node; the selector gives you a node set with only one node ... Thus, within a set with only one node, uniqueness is guaranteed, since an attribute with a given name can happen only once. This should explain why you cannot understand how you think it should work.

When you set it to the parent level, it works, because now you apply uniqueness among a set of child nodes.

In database terms, a constraint, such as what you need, can only be defined at the table level. This is how it will look (I rewrote the XSD a bit to get a good E / R from it).

XSD:

 <?xml version="1.0" encoding="utf-8" ?> <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:tns="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Parent" type="Parent"> <xs:unique name="ParentKey"> <xs:selector xpath="tns:Child"/> <xs:field xpath="@Key"/> </xs:unique> </xs:element> <xs:complexType name="Parent"> <xs:sequence> <xs:element name="Child" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="Key" type="xs:string" use="required"/> </xs:complexType> <xs:unique name="ChildKey"> <xs:selector xpath="."/> <xs:field xpath="@Key"/> </xs:unique> </xs:element> </xs:sequence> </xs:complexType> </xs:schema> 

XSD Chart:

enter image description here

Equivalent ADO.NET E / R:

enter image description here

XML showing errors:

 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> <Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd"> <Child Key="Key1"/> <Child Key="Key1"/> </Parent> 

Error message:

 Error occurred while loading [], line 5 position 3 There is a duplicate key sequence 'Key1' for the 'http://tempuri.org/XMLSchema.xsd:ParentKey' key or unique identity constraint. Unique.xml is invalid. 
+8
source

Source: https://habr.com/ru/post/1390203/


All Articles