Introduction to Relations in XML Schema (Page 1 of 5 )
If you are new to XML Schema, I strongly suggest you go through my series “Designing your own XML Schema.” It will introduce you to all the necessary concepts for designing XML schemas, even if you are a beginner.
A simple “Primary Key” in XML Schema
What is a Primary Key? Anyone familiar with database design would understand it right away. A Primary Key is a special type of constraint (or restriction) which makes sure that all the values are unique and without nulls (you can forget about indexing in XML here). This topic involves a bit of XPATH during the implementation of a primary key in XML schema.
For example, all the Employee IDs should never repeat and are compulsory. 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:key name="PK_Employee_Empno">
<xs:selector xpath=".//Employee" />
<xs:field xpath="Empno" />
</xs:key>
Within the above code fragment, you can see the definition of “Key.” I provided some name (“PK_Employee_Empno”) to identify the key. I also specified that the element “Empno” (in “Employee” element) is the one which should be applicable for the “Key.” You can also provide more than one element as part of the “Key,” making it a composite primary key. I examine this kind of key in the next section.