What is the difference between using xsd:unique and xsd:key?
The xs:unique Component. Defines that an element or an attribute value must be unique within the scope.
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.
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 .
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.
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.
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
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