Home arrow XML arrow Page 2 - Introduction to Relations in XML Schema

TOOLS YOU CAN USE

advertisement
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.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
      <xs:element name="OrderDetailsInfo">
            <xs:annotation>
                  <xs:documentation>Contains All Order details information</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="OrderDetails" maxOccurs="unbounded">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="OrderID" type="xs:int" />
                                          <xs:element name="ItemID" type="xs:int" />
                                          <xs:element name="Quantity" type="xs:int" />
                                          <xs:element name="UnitPrice" type="xs:float" />
                                          <xs:element name="TotalPrice" type="xs:float" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
            <xs:key name="PK_OrderDetails">
                  <xs:selector xpath=".//OrderDetails" />
                  <xs:field xpath="OrderID" />
                  <xs:field xpath="ItemID" />
            </xs:key>
      </xs:element>
</xs:schema>

The most important code fragment to concentrate on from the above schema is as follows:

            <xs:key name="PK_OrderDetails">
                  <xs:selector xpath=".//OrderDetails" />
                  <xs:field xpath="OrderID" />
                  <xs:field xpath="ItemID" />
            </xs:key>

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.


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 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials