Introduction to Relations in XML Schema - A composite “Primary Key” in XML Schema (Page 2 of 5 )
In our previous section, we discussed a simple “Primary Key” (or a primary key based on a single element). Now, we shall work on a composite primary key.
When we define a primary key on more than one element, we can call it a composite primary key. We need to make sure that the two elements are siblings to the same parent before enforcing the composite primary key.
For example, all the “OrderIDs” and “ItemIDs” would never repeat within the same order. How do I enforce this? Let us look into the following complete schema which does the same.
The most important code fragment to concentrate on from the above schema is as follows:
<xs:selector xpath=".//OrderDetails" />
<xs:field xpath="OrderID" />
<xs:field xpath="ItemID" />
Within the above code fragment, you can see the definition of “Key.” I provided some name (“PK_OrderDetails”) to identify the key. I also specified that the elements “OrderID” and “ItemID” (in the “OrderDetails” element) are the ones which should be applicable for the “Key.” Another issue to remember is that they can repeat individually, and cannot repeat together.