Home arrow XML arrow Page 4 - 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 - Combining two different roots or hierarchies in a single XML Schema: the solution
(Page 4 of 5 )

Now I am going to solve the problem explained in the previous section.  The problem could be solved in several ways.  But I prefer the following method:

<?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: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>

To understand the above schema, you should be familiar with “user-defined complex types” in XML schema.  If you are not, I strongly suggest that you go through my series “Designing your own schema.”

I shall explain the above schema below.

Combining two different roots or hierarchies in a single XML Schema: explanation

This section explains the schema introduced in the previous section.  Instead of explaining it all at once, let me explain it part by part.  Let us consider the following fragment:

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

The above code fragment defines a new user-defined complex type named “DeptType.”  It internally contains three elements, namely “Deptno,” “Dname” and “Loc.”  There exists another code fragment very similar to the one above:

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

The above code fragment defines a new user-defined complex type named “EmployeeType.”  It internally contains four elements, namely “Empno,” “Ename,” “Sal” and “Deptno.”  The above two are simply “data types.”  We need to declare elements based on the above user-defined data types.  Let us consider the following code fragment:

      <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:element>

Within the above code fragment, you can observe that I defined a new root element called “Organization.”  Within this root element, I defined two more elements, “dept” and “Employee,” based on the user-defined complex types explained previously.  This creates a meaningful hierarchy having only one root at the top and any number of hierarchies from within the root element.  What about our primary keys?  They are defined as follows:

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


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