tags:

views:

577

answers:

3

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

A: 

xsd:key is unique AND is used for a collection of elements a key within a scope xsd:unique is just unique within a scope.

Hope this helps...

norbertB
So when i declare multiple xsd:keys on different elements, all keys defined must still be unique?
Joepie
No, If you got a collection of book elements and you declared xsd:key to be the isbn attribute of the book element in the schema. Then the values of the isbn attribute should be unique. this has no influence on other elements with other keys
norbertB
But xsd:unique also does this. xsd:key is also scoped. I can find no differences...?
Joepie
I think the only difference is that xsd:key is not nillable, xsd:unique is...http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness
Joepie
A: 

it is similar to database.

key field is like primarykey.. unique and id

unique field is not a key but it has to be unique...

ufukgun
Oke, but what does it add to the xsd that a key is also an "id"?
Joepie
A: 

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

Joepie