xs:key and xs:keyref models the relationship between primary key and foreign key in a database. xs:key ensures that the pieces of data in an XML document are unique and not null . They enforce data integrity in the XML Document.
xs:unique and xs:ID
The xs:unique element and the xs:ID datatype ensure that the pieces of data in an XML document are unique
XML Schema Exercise:
1. Create a Cart element. Each Cart element must have multiple instances of child Category element
2. Category element can have multiple instances of child Book and Sales element n
3. Each Book element must have a BookNumber attribute whose value is unique within Category.
4. Each Sales must have an ID attribute whose value matches one of these unique BookNumbers
xs:selector and xs:field
The identity constraint “keyBookNumber” does not allow the incoming xml files to have duplicate values for book number.
xs:selector: The context in which the primary key constraint should be applied
xs:field: The field that uniquely identifies the record
After applying the primary key constraint xs:key the XML Schema Document will look like this:
Now lets see the relationship between the book and sales records. As per the exercise ” each Sales must have an ID attribute whose value matches one of these unique BookNumbers”
Sales ID attribute must match anyone of the unique BookNumber attribute of Book. Use xs:keyref to create this relationship.