XML Schema – xs:key and xs:keyref

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.

xskey

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

xsid

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

xsselector

After applying the primary key constraint xs:key the XML Schema Document will look like this:

Schemaexercise1

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”

 

relationship

Sales ID attribute must match anyone of the unique BookNumber attribute of Book. Use xs:keyref to create this relationship.

output

 

 

Add Your Comments

Required
Required
Tips

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <ol> <ul> <li> <strong>

Your email is never published nor shared.

Ready?