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