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

+----------------------------------------------------------------------+
| `<span style="font-size:large;">`{=html}`<b>`{=html}Learning         |
| objectives`</b>`{=html}`</span>`{=html}\                             |
|                                                                      |
| -   `<span style="font-size:medium;">`{=html}Create a schema for a   |
|     data model containing a 1:1 relationship `</span>`{=html}        |
| -   `<span style="font-size:medium;">`{=html}Place restrictions on   |
|     elements or attributes in an XML schema`</span>`{=html}          |
| -   `<span style="font-size:medium;">`{=html}Specify fixed or        |
|     default values for an element in an XML schema`</span>`{=html}   |
+----------------------------------------------------------------------+

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

![](xmldm1to1.png "xmldm1to1.png")

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

``` XML
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> 
<!--
Tour Guide
--> 
 <xsd:element name="tourGuide"> 
  <xsd:complexType> 
   <xsd:sequence> 
    <xsd:element name="country" type="countryDetails" minOccurs="1" maxOccurs="unbounded" /> 
   </xsd:sequence> 
  </xsd:complexType> 
 </xsd:element> 
<!--
Country
--> 
 <xsd:complexType name="countryDetails"> 
  <xsd:sequence> 
   <xsd:element name="countryName" type="xsd:string" minOccurs="1" maxOccurs="1"/> 
   <xsd:element name="population" type="xsd:integer" minOccurs="0" maxOccurs="1" default="0"/> 
   <xsd:element name="continent" minOccurs="0" maxOccurs="1"> 
    <xsd:simpleType> 
     <xsd:restriction base="xsd:string"> 
      <xsd:enumeration value="Asia"/> 
      <xsd:enumeration value="Africa"/> 
      <xsd:enumeration value="Australasia"/> 
      <xsd:enumeration value="Europe"/> 
      <xsd:enumeration value="North America"/> 
      <xsd:enumeration value="South America"/> 
      <xsd:enumeration value="Antarctica"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
   </xsd:element> 
   <xsd:element name="topDestination" type="destinationDetails" minOccurs="0" maxOccurs="1"/> 
   <xsd:element name="destination" type="destinationDetails" minOccurs="0" maxOccurs="unbounded"/> 
  </xsd:sequence> 
 </xsd:complexType> 
<!--
Destination
--> 
 <xsd:complexType name="destinationDetails"> 
  <xsd:all> 
   <xsd:element name="destinationName" type="xsd:string"/> 
   <xsd:element name="description" type="xsd:string"/> 
   <xsd:element name="streetAddress" type="xsd:string" minOccurs="0"/> 
   <xsd:element name="telephoneNumber" type="xsd:string" minOccurs="0"/> 
   <xsd:element name="websiteURL" type="xsd:anyURI" minOccurs="0"/> 
  </xsd:all> 
 </xsd:complexType> 
</xsd:schema> 
```

\
`<small>`{=html}**Exhibit 4-2: XML Schema for a one-to-one
relationship**`</small>`{=html}

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

``` XML
<xsd:element name="streetAddress">
 <xsd:simpleType>
  <xsd:restriction base="xsd:string">
   <xsd:whiteSpace value="preserve"/>
  </xsd:restriction>
 </xsd:simpleType>
</xsd:element>
```

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

:   

    :   `<xsd:whiteSpace value="replace"/>`{=html}

-   Collapse -- The processor will remove all whitespace characters.

:   

    :   `<xsd:whiteSpace value="collapse"/>`{=html}

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

:   

    :   `<xsd:length value="8"/>`{=html}
    :   `<xsd:minLength value="5"/>`{=html}
    :   `<xsd:maxLength value="8"/>`{=html}

#### 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 `<all>`{=html} indicator specifies by default that the child
elements can appear in any order and that each child element must occur
once and only once:

``` XML
<xsd:element name="person">
 <xsd:complexType>
  <xsd:all>
   <xsd:element name="firstname" type="xsd:string"/>
   <xsd:element name="lastname" type="xsd:string"/>
  </xsd:all>
 </xsd:complexType>
</xsd:element>
```

##### Choice indicator

The `<choice>`{=html} indicator specifies that either one child element
or another can occur:

``` XML
<xsd:element name="person">
 <xsd:complexType>
  <xsd:choice>
   <xsd:element name="employee" type="employee"/>
   <xsd:element name="visitor" type="visitor"/>
  </xsd:choice>
 </xsd:complexType>
</xsd:element>
```

##### Sequence indicator

The `<sequence>`{=html} indicator specifies that the child elements must
appear in a specific order:

``` XML
<xsd:element name="person">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="firstname" type="xsd:string"/>
   <xsd:element name="lastname" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>
```

## 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
`<topDestination>`{=html} can appear in any order because of the
`<xsd:all>`{=html} order indicator used in the schema.

``` XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="newXMLSchema.xsl" media="screen"?>
<tourGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="XMLSchema.xsd">   
<!--
Malaysia
-->   
<country> 
   <countryName>Malaysia</countryName> 
   <population>22229040</population> 
   <continent>Asia</continent> 
   <topDestination> 
    <description>A popular duty-free island north of Penang.</description> 
    <destinationName>Pulau Langkawi</destinationName> 
   </topDestination> 
   <destination> 
    <destinationName>Muzium Di-Raja</destinationName> 
    <description>The original palace of the Sultan</description>
    <streetAddress>122 Muzium Road</streetAddress>
    <telephoneNumber>48494030</telephoneNumber>
    <websiteURL>www.muziumdiraja.com</websiteURL> 
   </destination> 
   <destination> 
    <destinationName>Kinabalu National Park</destinationName> 
    <description>A national park</description>
    <streetAddress>54 Ocean View Drive</streetAddress>
    <telephoneNumber>4847101</telephoneNumber>
    <websiteURL>www.kinabalu.com</websiteURL> 
   </destination> 
  </country>
<!--
Belize
--> 
  <country> 
   <countryName>Belize</countryName> 
   <population>249183</population> 
   <continent>South America</continent> 
   <topDestination> 
    <destinationName>San Pedro</destinationName> 
    <description>San Pedro is an island off the coast of Belize</description> 
   </topDestination> 
   <destination> 
    <destinationName>Belize City</destinationName> 
    <description>Belize City is the former capital of Belize</description>
    <websiteURL>www.belizecity.com</websiteURL> 
   </destination> 
   <destination> 
    <destinationName>Xunantunich</destinationName> 
    <description>Mayan ruins</description>
    <streetAddress>4 High Street</streetAddress>
    <telephoneNumber>011770801</telephoneNumber> 
   </destination> 
  </country> 
  </tourGuide>
```

`<small>`{=html}**Exhibit 4-3: XML Document for a one-to-one
relationship**`</small>`{=html}

## Summary

  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Schema designers may place restrictions on the length of elements and on how the processor handles white space. Schema designers may also specify fixed or default values for an element. Order indicators can be used to specify the order in which elements must appear in an XML document.
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Exercises

\

## Answers

