XML
  Home arrow XML arrow Page 6 - One-One, One-Many and Many-Many Relations ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

One-One, One-Many and Many-Many Relations in XML Schema
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2006-04-24

    Table of Contents:
  • One-One, One-Many and Many-Many Relations in XML Schema
  • Implementing a “One to One” relation in XML Schema: schema explained
  • Implementing a “One to One” relation in XML Schema: a sample XML document
  • Implementing a “One to Many” relation in XML Schema: the schema
  • Implementing a “One-Many” relation in XML Schema: a sample XML document
  • Implementing a Many to Many relation in XML Schema: the schema

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    One-One, One-Many and Many-Many Relations in XML Schema - Implementing a Many to Many relation in XML Schema: the schema


    (Page 6 of 6 )

    Until now, we have gone through only one to one and one to many relations.  Now, it is time to look at implementing a many to many relation.  Many to many relations are a bit complicated when compared with the above two. 

    For the sake of this example, I am taking the same concept of the “pubs” database (demonstration database) existing in Microsoft SQL Server.  Concisely, it has three tables: “Titles,” “Authors” and “TitleAuthors”.  It also has relations in between three of them.  I would like to have the same concept implemented in my schema (instead of in the database).

    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:element name="pubs">
                <xs:complexType>
                      <xs:sequence>
                            <xs:element name="Authors"
     type="TypeAuthors" maxOccurs="unbounded" />
                            <xs:element name="Titles"
     type="TypeTitles" maxOccurs="unbounded" />
                            <xs:element name="TitleAuthors"
     type="TypeTitleAuthor" maxOccurs="unbounded" />
                      </xs:sequence>
                </xs:complexType>
    .
    .
    .
          <xs:complexType name="TypeTitles">
                <xs:sequence>
                      <xs:element name="Title_ID" type="xs:string" />
                      <xs:element name="Title" type="xs:string" />
                </xs:sequence>
          </xs:complexType>
          <xs:complexType name="TypeAuthors">
                <xs:sequence>
                      <xs:element name="au_id" type="xs:string" />
                      <xs:element name="au_lname" type="xs:string" />
                      <xs:element name="au_fname" type="xs:string" />
                </xs:sequence>
          </xs:complexType>
          <xs:complexType name="TypeTitleAuthor">
                <xs:sequence>
                      <xs:element name="au_id" type="xs:string" />
                      <xs:element name="Title_ID" type="xs:string" />
                </xs:sequence>
          </xs:complexType>
    </xs:schema>

    Finally, here are the primary keys and foreign keys -- the leftovers of the schema presented above.  The following is the fragment schema you need to add to the schema defined above (at the appropriate place only).

                <xs:key name="PK_Authors">
                      <xs:selector xpath=".//Authors" />
                      <xs:field xpath="au_id" />
                </xs:key>
                <xs:key name="PK_Titles">
                      <xs:selector xpath=".//Titles" />
                      <xs:field xpath="Title_ID" />
                </xs:key>
                <xs:key name="PK_TitleAuthors">
                      <xs:selector xpath=".//TitleAuthors" />
                      <xs:field xpath="au_id" />
                      <xs:field xpath="Title_ID" />
                </xs:key>
                <xs:keyref name="FK_Authors_TitleAuthors"
     refer="PK_Authors">
                      <xs:selector xpath=".//TitleAuthors" />
                      <xs:field xpath="au_id" />
                </xs:keyref>
                <xs:keyref name="FK_Titles_TitleAuthors"
     refer="PK_Titles">
                      <xs:selector xpath=".//TitleAuthors" />
                      <xs:field xpath="Title_ID" />
                </xs:keyref>

    You can observe carefully that I have three primary keys and two foreign keys to represent a “many-many” relation (of course, you can have even more).

    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.

       · Hello guys, this is another extension to the XML Schema series. Enjoy it.
     

    XML ARTICLES

    - 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
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway