Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between xsd:key and xsd:unique

Tags:

xml

xsd

What is the difference between using xsd:unique and xsd:key?

like image 424
Joep Avatar asked Aug 10 '09 07:08

Joep


People also ask

What is xs unique?

The xs:unique Component. Defines that an element or an attribute value must be unique within the scope.

What is an XML key?

Definition and Usage The key element specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document.

What is xs key?

xs:key is used to define simple or compound keys by unambiguously identifying each element of a selected list of subelements within the scope of a root element. xs:key is very similar to xs:unique .

What does Xs mean in XSD?

XML Schema Definition or XSD is a recommendation by the World Wide Web Consortium (W3C) to describe and validate the structure and content of an XML document. It is primarily used to define the elements, attributes and data types the document can contain.


2 Answers

xsd:unique does not require the field to exist for the selector. If the field exists, its value should be unique.

xsd:key requires the field to exist for all selector results. Every element in the selector should have a key.

Note that xsd:keyref is not a requirement for xsd:key, the xsd:key can be there withoug any xsd:keyref.

like image 189
Bruno Ranschaert Avatar answered Oct 19 '22 19:10

Bruno Ranschaert


I found my own answer:

It all has to do with cardinality:

xsd:key is used for (0..N) and (1..1) multiplicity. xsd:unique is used for optional (0..1) multiplicity. This is also the reason why a the selected field in a xsd:unique can be nill.

Source of my answer: http://www.xml.com/lpt/a/987

like image 7
Joep Avatar answered Oct 19 '22 19:10

Joep