Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make an attribute unique in xml schema?

Tags:

xml

xsd

i wanna make an attribute of an element to be unique like primary key. how to make it?

like image 474
brainless Avatar asked Aug 02 '10 11:08

brainless


People also ask

What is unique attribute in XML?

The unique element defines that an element or an attribute value must be unique within the scope. The unique element MUST contain the following (in order): one and only one selector element (contains an XPath expression that specifies the set of elements across which the values specified by field must be unique)

Can XML tags have attributes?

XML elements can have attributes, just like HTML.

Why do you avoid XML attributes?

There are 3 main reasons to avoid attributes:Attributes are not easily expandable. If you want to change in attribute's vales in future, it may be complicated. Attributes cannot describe structure but child elements can. Attributes are more difficult to be manipulated by program code.


2 Answers

Something like this should work:

<xs:element name="books" maxOccurs="unbounded">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="book" maxOccurs="unbounded">
            <xs:complexType>
               <xs:attribute name="isbn" type="xs:string"/>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
   </xs:complexType>
   <xs:unique name="unique-isbn">
      <xs:selector xpath="book"/>
      <xs:field xpath="@isbn"/>
   </xs:unique>
</xs:element>

Basically, you can define a "uniqueness" constraint using a <xs:unique> element and define what XPath this uniqueness should apply to.

See W3Schools' entry on <xs:unique> for more info.

like image 124
marc_s Avatar answered Sep 22 '22 05:09

marc_s


Note: This is not working if you have different namespaces. Then you need the full XPath expression:

This could be like:

<xs:unique name="unique-isbn">
      <xs:selector xpath="theOtherNamespace:book"/>
      <xs:field xpath="@isbn"/>
</xs:unique>
like image 38
hpu Avatar answered Sep 21 '22 05:09

hpu