XML - Managing Data Exchange/Recursive relationships

Introduction
Recursive relationships are an interesting and more complex concept than the relationships you have seen in the previous chapters. A recursive relationship occurs when there is a relationship between an entity and itself. For example, a one-to-many recursive relationship occurs when an employee is the manager of other employees. The employee entity is related to itself, and there is a one-to-many relationship between one employee (the manager) and many other employees (the people who report to the manager). Because of the more complex nature of these relationships, we will need slightly more complex methods of mapping them to a schema and displaying them in a style sheet.

The one-to-one recursive relationship
Continuing with the tour guide model, we will develop a schema that shows cities that have hosted the Olympics and the previous host city. Since the previous host is another city and only one city can be the previous host this is a one to one recursive relationship.
 * [[Image:xml_host.png|Recursive ]]

host.xsd (XML schema for a one-to-one recursive model)
Exhibit 1: XML schema for Host City Entity

host.xml (XML document for a one-to-one recursive model)
Exhibit 2: XML Document for Olympic Host City

The one-to-many recursive relationship
A hypothetical sports team is divided into squads with each squad having a captain. Every person on the team is a player, regardless of whether they are a squad captain. Since a squad captain is a player, this situation meets the definition of a recursive relationship—a squad captain is also a player and has a one-to-many relationship with the other players. This is a one-to-many recursive relationship because one captain has many players under him/her. See the example below for how to model the relationship.

team.xsd (XML schema for a one-to-many recursive model)
Exhibit 3: XML schema for Team Entity 

team.xml (XML document for a one-to-many recursive model)
Exhibit 4: XML Document for Team Entity 

Natural one-to-many recursive structure
A more natural approach for most one-to-many recursive relationships is to use XML's hierarchical nature to directly represent the heirarchy. Consider Locations:

The many-to-many recursive relationship
Think you're getting a feel for recursive relationships yet? Well, there is still the third and final relationship to add to your repertoire — the many-to-many recursive. A common example of a many-to-many recursive relationship is when one item can be comprised of many items of the same data type as itself, and each of those sub-items may belong to another parent item of the same data type. Sound confusing? Let's look at the example of a product that can consist of a single item or multiple items (i.e., a packaged product). The example below describes tourist products that can be packaged together to create a new product.

product.xsd (XML schema for a many-to-many recursive model)
Exhibit 5: XML schema for Product Entity 

product.xml (XML document for a many-to-many recursive model)
Exhibit 6: XML Document for Product Entity 

Summary
When the child has the same type of data as its parent in a parent-child type data relationship, this is a sign of the existence of a recursive relationship. The xsd:ID and xsd:IDREF elements can be used in a schema to create primary key-foreign key values in an XML document.

Answers
External Links
 * XML Schema