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



Previous Chapter Next Chapter
The one-to-many relationship The many-to-many relationship



Learning objectives

  • Create a schema for a data model containing a 1:1 relationship
  • Place restrictions on elements or attributes in an XML schema
  • Specify fixed or default values for an element in an XML schema


Introduction

edit

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

edit

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

edit

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

edit
<?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>


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

New elements in schema

edit


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

edit


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:

<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

edit

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"/>
  • Collapse – The processor will remove all whitespace characters.
<xsd:whiteSpace value="collapse"/>
  • Length, maxLength, minLength—the length of the element can be fixed or can have a predefined range.
<xsd:length value="8"/>
<xsd:minLength value="5"/>
<xsd:maxLength value="8"/>

Order indicators

edit

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

All indicator
edit

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

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

The <choice> indicator specifies that either one child element or another can occur:

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

The <sequence> indicator specifies that the child elements must appear in a specific order:

<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

edit


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> can appear in any order because of the <xsd:all> order indicator used in the schema.

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

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

Summary

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