Home arrow XML arrow Page 4 - One-One, One-Many and Many-Many Relations in XML Schema
SEARCH DEVARTICLES

One-One, One-Many and Many-Many Relations in XML Schema - Implementing a “One to Many” relation in XML Schema: the schema
(Page 4 of 6 )

Until now, we have looked at only the one to one relation.  Now, it is time to look at implementing the one to many (or conversely many to one) relation. 

Let us consider the following schema.

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

I already explained similar types of hierarchies in my previous sections; I would like to show you the most important bits now.  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.”  It became a one to many relation because the “deptno” in “Employee” is not a primary key (or unique key).

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


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