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

* Chapters
* Appendices
* Exercises
* Computer Science Home
* Library and Information Science Home
* Markup Languages
* To do list
* Contributors list
* Contributing to Wikibooks
| Previous Chapter | Next Chapter |
| ← The one-to-one relationship | Recursive relationships → |
* Learning objectives
* Learn different methods to represent a many-to-many relationship using XML
* Create XML schemas using the "Eliminate" and "ID/IDREF" methods to structure content based on a many-to-many relationship
* Create the corresponding XML documents for the "Eliminate" and "ID/IDREF" methods
* Learn to use the key function in an XML stylesheet to format data structured with the "ID/IDREF" method
* Create a basic XML stylesheet that incorporates the key function

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

## Solutions: many-to-many relationship
### 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 <xsl:key> 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

## Expedia.de: XML and affiliate marketing

* Expedia.de is the German subsidiary of expedia.com, the internet-based travel agency headquartered in Bellevue, Washington, USA. It offers its customers the booking of airline tickets, car rentals, vacation packages and various other attractions and services via its website and by phone. Its websites attract more than 70 million visitors each month. Currently expedia.com employs 4.600 employees serving customers in the United States, Canada, the UK, France, Germany, Italy, and Australia.
* For marketing purposes expedia.de set up an affiliate marketing program. Affiliate marketing is a way to reach potential customers without any financial risk for the company intending to advertise (merchant). The merchant gives website owners, which are called affiliates, the opportunity to refer to the merchant page, offering commission-based monetary rewards as incentives. In the case of Expedia.de the affiliate partners receive a commission every time users from their websites book travel on expedia.de. So the affiliates can concentrate on selling and the merchant takes care of handling the transactions.
* To ease the business of the affiliate partners – and of course to make the program more attractive – Expedia.de offers its partners a service called xmlAdEd. xmlAdEd is a service providing current product information on using XML. Affiliates using this service are able to request more than 8 million of travel offerings in XML format via HTTP-request. The data is updated several times a day.
* In the HTTP-request you can set certain parameters such as location, price, airport code, ...
* The use of XML in this case gives the affiliates several advantages:
* - Efficient and flexible processing of the data because of separation of structure, content and style.
* - Platform-independent processing of the data.
* - Lossless conversion into other file formats.
* - Easy integration in their websites.
* - Possibility to create an own web shop in individual design
* By providing their affiliates product information in XML, expedia.de not only eases the business of their partners, but also ensures that customers receive consistent, up-to-date information on their services.

### Summary
## References

http://www-128.ibm.com/developerworks/xml/library/x-xdm2m.html
http://www.w3.org/TR/xslt#key