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

Introduction
In the previous chapter, some new features of XML schemas, documents, and stylesheets were introduced as well as how to model a one-to-many relationship. In this chapter, we will introduce the modeling of a one-to-one relationship in XML. We will also introduce more features of an XML schema.

A one-to-one (1:1) relationship
The following diagram shows a one-to-one and a one-to-many relationship. The one-to-one relationship records each country as a single top destination.



Exhibit 4-1: Data model for a 1:1 relationship

XML schema
A one-to-one (1:1) relationship is represented in the data model in Exhibit 4-1. The addition of country and destination to the data model allows the 1:1 relationship named topDestination. A country has many different destinations, but only one top destination. The XML schema in Exhibit 4-2 shows how to represent a 1:1 relationship in an XML schema.

XML schema example
Exhibit 4-2: XML Schema for a one-to-one relationship 

New elements in schema
Let’s examine the new elements and attributes in the schema in Exhibit 4-2.
 * Country is a complex type defined in City to represent the 1:M relationship between a country and its cities.
 * Destination is a complex type defined in Country to represent the 1:M relationship between a country and its many destinations.
 * topDestination is a complex type defined in Country to represent the 1:1 relationship between a country and its top destination.

Restrictions in schema
Placing restrictions on elements was introduced in the previous chapter; however, there are more potentially useful restrictions that can be placed on an element. Restrictions can be placed on elements and attributes that affect how the processor handles whitespace characters:

White space & length constraints
The whiteSpace constraint is set to "preserve", which means that the XML processor will not remove any white space characters. Other useful restrictions include the following:


 * Replace – the XML processor will replace all whitespace characters with spaces.
 * 


 * Collapse – The processor will remove all whitespace characters.
 * 


 * Length, maxLength, minLength—the length of the element can be fixed or can have a predefined range.
 * 
 * 
 * 

Order indicators
In addition to placing restrictions on elements, order indicators can be used to define in what order elements should occur.

All indicator
The indicator specifies by default that the child elements can appear in any order and that each child element must occur once and only once:

Choice indicator
The indicator specifies that either one child element or another can occur:

Sequence indicator
The indicator specifies that the child elements must appear in a specific order:

XML document
The XML document in Exhibit 4-3 shows how the new elements (country and destination) defined in the XML schema found in Exhibit 4-2 are used in an XML document. Note that the child elements of  can appear in any order because of the  order indicator used in the schema.

Exhibit 4-3: XML Document for a one-to-one relationship