i wanna make an attribute of an element to be unique like primary key. how to make it?
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)
XML elements can have attributes, just like HTML.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With