Home arrow XML arrow Page 6 - Designing Your own XML Schema: Constraining with Restrictions
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Designing Your own XML Schema: Constraining with Restrictions - Defining a Primary Key in XML Schema
(Page 6 of 6 )

This topic is a bit more complicated than any of the previous sections, as it involves a bit of XPATH as well.  What is a Primary Key?  Anyone familiar with database design would understand it on the spot.  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).

For example, all the Employee IDs should never repeat and are compulsory.  How do I enforce it? Let us look into the following complete schema, which does the same.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
      <xs:element name="Employees">
            <xs:annotation>
                  <xs:documentation>Contains All Employee information</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="Employee" maxOccurs="unbounded">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="ID" type="xs:string" />
                                          <xs:element name="Name" type="xs:string" />
                                          <xs:element name="Age" type="xs:int" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
            <xs:key name="PK_Employee_ID">
                  <xs:selector xpath=".//Employee" />
                  <xs:field xpath="ID" />
            </xs:key>
      </xs:element>
</xs:schema>



Within the above code, you can see the definition of “Key.”  I provided a name (“PK_Employee_ID”) to identify the key.  I also specified that the element “ID” (in the “Employee” element) is the one which should be applicable to the “Key.”  You can also provide more than one element as part of the “Key” (making it a composite primary key).

Any comments, suggestions, feedback, bugs, errors are highly appreciated at jag_chat@yahoo.com


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
XML ARTICLES

- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath
- Simple Web Syndication with RSS 2.0
- Java UI Design with an IDE

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials