XML - Managing Data Exchange/The many-to-many relationship

Introduction
In the previous chapters, you learned how to use XML to structure and format data based on one-to-one and one-to-many relationships. Because XML provides the means to model data using hierarchical parent-child relationships, the one-to-one and one-to-many relationships are relatively simple to represent in XML. However, this hierarchical parent-child structure is difficult to use to model the many-to-many relationship, a common relationship between entities in many situations.

In this chapter, we will explore the pros and cons of a few methods that are used to model a many-to-many relationship in XML; these methods offer compromises in overcoming the problems that arise when applying this relationship to XML. In particular, we will see examples of how to model the many-to-many relationship using two different methods, "Eliminate" and "ID/IDREF." Additionally, in the XML stylesheet, we will learn how to implement the key function to display the data that was modeled using the "ID/IDREF" method.

Problems: many-to-many relationship
In XML, the parent-child relationship is most commonly used to represent a relationship. This can easily be applied to a one-to-one or one-to-many relationship. A many-to-many relationship is not supported directly by XML; the parent-child relationship will not work as each element may only have a single parent element. There are couple of possible solutions to get around this.

Eliminate
Create XML documents that eliminate the need for a many-to-many relationship By limiting the extent of information that is conveyed, you can get around the need for a many-to-many relationship. Instead of trying to have one XML document encompass all of the information, separate the information where one document describes only one of the entities that participates in the many-to-many relationship. Using our tourGuide relationship for example, one way for us to accomplish this would be creating a separate XML document for each hotel. The relationship with amenity would ultimately then become a one-to-many. This method is more suitable for situations in which the scope of data exchange can be limited to subsets of data. However, using this method for more broadly scoped data exchange, you may repeat data several times, especially if there are many attributes. To avoid this redundancy, use the ID/IDREF method.

ID/IDREF
Represent the many-to-many relationship using unique identifiers Although not the most user-friendly way to handle this problem, one way of getting around the many-to-many relationship is by creating keys that would uniquely identify each entity. To do this, an element with ID or IDREF attributes-types must be specified within the XML schema. To use a data modeling analogy, ID is similar to the primary key, and IDREF is similar to the foreign key.

Many-to-many relationship data model
Exhibit 1: Data model for a m:m relationship

The relationship reads, a hotel can have many amenities, and an amenity can exist at many hotels.

As you will notice, in order to represent a many-to-many relationship, two entities were added. The middle entity is necessary for the data model to represent an associative entity that stores data about the relationship between hotel and amenity. Using our Tour Guide example, "Amenity" was added to represent a list of possible amenities that a hotel can possess.

The following examples illustrate methods to represent a many-to-many relationship in XML.

Eliminate: sample solution
In this example, the many-to-many relationship has been converted to a one-to-many relationship.

XML schema
Exhibit 2: XML schema for "Eliminate" method

XML document
Exhibit 3: XML document for "Eliminate" method

ID/IDREF: sample solution
To avoid redundancy, we create a separate element, "amenity," which is included at the top of the schema along with "hotel." Remember, the data types ID and IDREF are synonymous with the primary key and foreign key, respectively. For every foreign key (IDREF), there must be a matching primary key (ID). Note that the IDREF data type has to be an alphanumeric string.

The following example illustrates the ID/IDREF approach. Notice that the ID for the amenity pool is defined as "k1," and every hotel with a pool as an amenity references "k1," using IDREF. If the IDREF does not match any ID, then the document will not validate.

XML schema
Exhibit 4: XML schema for "ID/IDREF" method

XML document
Exhibit 5: XML document for "ID/IDREF" method

Key function: XML stylesheet
In order to set up an XML stylesheet using the ID/IDREF method for a many-to-many relationship, the key function should be used. In the stylesheet, the &lt;xsl:key&gt; element specifies the index, which is used to return a node-set from the XML document.

A key consists of the following:

1. the node that has the key 2. the name of the key 3. the value of a key

The following XML stylesheet illustrates how to use the key function to present content that is structured in a many-to-many relationship.

XML stylesheet
Exhibit 6: XML stylesheet for "ID/IDREF" method