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

Introduction to Relations in XML Schema


This is the first article in a series concentrating on implementing relations for designing robust XML schema definitions.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 15
April 17, 2006
TABLE OF CONTENTS:
  1. · Introduction to Relations in XML Schema
  2. · A composite “Primary Key” in XML Schema
  3. · Combining two different roots or hierarchies in a single XML Schema: the problem
  4. · Combining two different roots or hierarchies in a single XML Schema: the solution
  5. · Defining a simple relation in XML Schema: XML Schema example

print this article
SEARCH DEVARTICLES

Introduction to Relations in XML Schema - Defining a simple relation in XML Schema: XML Schema example
(Page 5 of 5 )

Till now we have gone through defining primary keys and combining hierarchies within a root element.  Now it is time to look at the concept of “relation.”

Every developer knows what a “relation” means in an RDBMS.  The terms “referential integrity” or “foreign key” are also used for this concept in RDBMS theory.  We can even enforce the same types of relations between hierarchies of elements within the same XML schema (or external schemas as well).

Enforcing a relation is quite straightforward in XML schema, when we know how to implement a “primary key.”  Let us modify the schema presented in the previous section to enforce a relation between “dept” and “Employee” elements:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
      <xs:complexType name="DeptType">
            <xs:sequence>
                  <xs:element name="Deptno" type="xs:int"/>
                  <xs:element name="Dname" type="xs:string"/>
                  <xs:element name="Loc" type="xs:string"/>
            </xs:sequence>
      </xs:complexType>
      <xs:element name="Organization">
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="dept" type="DeptType" maxOccurs="unbounded"/>
                        <xs:element name="Employee" type="EmployeeType" 
maxOccurs="unbounded"/>
                  </xs:sequence>
            </xs:complexType>
            <xs:key name="PK_Dept_Deptno">
                  <xs:selector xpath=".//dept"/>
                  <xs:field xpath="Deptno"/>
            </xs:key>
            <xs:key name="PK_Employee_Empno">
                  <xs:selector xpath=".//Employee"/>
                  <xs:field xpath="Empno"/>
            </xs:key>
            <xs:keyref name="FK_deptEmployee" refer="PK_Dept_Deptno">
                  <xs:selector xpath=".//Employee"/>
                  <xs:field xpath="Deptno"/>
            </xs:keyref>
      </xs:element>
      <xs:complexType name="EmployeeType">
            <xs:sequence>
                  <xs:element name="Empno" type="xs:int"/>
                  <xs:element name="Ename" type="xs:string"/>
                  <xs:element name="Sal" type="xs:float"/>
                  <xs:element name="Deptno" type="xs:int"/>
            </xs:sequence>
      </xs:complexType>
</xs:schema>

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

            <xs:keyref name="FK_deptEmployee" refer="PK_Dept_Deptno">
                  <xs:selector xpath=".//Employee"/>
                  <xs:field xpath="Deptno"/>
            </xs:keyref>

The above code fragment says that the “deptno” in “Employee” should “refer” to the values in “PK_Dept_Deptno” (which is nothing but “deptno” in “dept”) for integrity.  We also named the relation “FK_deptEmployee.”

How about an XML document for the above XML schema?  The next section shows you that.

Defining a simple relation in XML Schema: XML document sample

In the previous section, I defined an XML schema to enforce a simple relation in between two element hierarchies.  The following is an XML document sample for the previously defined XML schema.

<?xml version="1.0" encoding="UTF-8"?>
<Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance
" xsi:noNamespaceSchemaLocation="C:Documents and
SettingsAdministratorDesktopEmployees.xsd
">
      <dept>
            <Deptno>10</Deptno>
            <Dname>Accounting</Dname>
            <Loc>Dallas</Loc>
      </dept>
      <dept>
            <Deptno>20</Deptno>
            <Dname>Production</Dname>
            <Loc>New York</Loc>
      </dept>
      <Employee>
            <Empno>1001</Empno>
            <Ename>Jag</Ename>
            <Sal>2500</Sal>
            <Deptno>10</Deptno>
      </Employee>
      <Employee>
            <Empno>1002</Empno>
            <Ename>Win</Ename>
            <Sal>2600</Sal>
            <Deptno>20</Deptno>
      </Employee>
</Organization>

The above XML document is error free as the values in the “deptno” element of “Employee” exist in the “deptno” element of “dept.”  Try changing “20” (in “Employee”) to “30” (which does not exist in “dept”).  The XML document becomes invalid during parsing!

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

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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