# XML - Managing Data Exchange/The one-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 |
| ← Basic data structures | The one-to-one relationship → |
* Learning objectives
* Learn different techniques of implementing one-to-many relationships in XML
* create custom data types in an XML schema
* create empty elements with attributes in an XML document
* define a presentation layout for an XML document using a table with varying background colors and font characteristics, and display images in an XML stylesheet

## Introduction

In a one-to-many relationship, one object can reference several instances of another. A model is mapped into a schema whereby each data model entity becomes a complex element type. Each data model attribute becomes a simple element type, and the one-to-many relationship is recorded as a sequence.
Exhibit 1:Data model for 1:m relationship
In the previous chapter, we introduced a simple XML schema, XML document, and an XML stylesheet for a single entity data model. We now include more features of each of the key aspects of XML.

### Implementing a one-to-many relationship

There are three different techniques for implementing a one-to-many relationship:
Containment relationship: A structure is defined where one element is contained within another. The "contained" element ceases to exist when the "container" element is removed. For instance, where a city has many hotels, the hotels are "contained" in the city.
Intra-document relationships: In a case where you have one city with many hotels, rather than a city containing hotels, a hotel will have a "location in" relationship to a city. A city id is used as a reference on the hotel element. Therefore, rather than the hotels being contained in the city, they now just reference the city's id via the cityRef attribute. This is very similar to a foreign key in a relational database.
Inter-document relationships: The inter-document relationship is much like the intra-document relationship. It also uses the id and idRef attributes to assign an attribute to a parent attribute. The difference is that the inter-document relationship is used when tables, such as the city and hotel tables, might live in different filesystems or tablespaces.
Exhibit 2:Checklist for deciding what technique to use:
| Technique | Passing Data | Flexibility | Ease of Use |
| Containment | Excellent | Fair | Excellent |
| Intra-Document | Good | Good | Good |
| Inter-Document | Fair | Excellent | Fair |

## XML schema

Some of the built-in data types for an XML schema were introduced in the previous chapter, but still, there are more that are very useful, such as anyURI, date, time, year, and month. In addition to the built-in data types, a custom data type can be defined by the schema designer to accept specific data input. As we have learned, data are defined in XML documents using markup tags defined in an XML schema. However, some elements might not have values. An empty element tag can be used to address this situation. An empty element tag (and any custom markup tag) can contain attributes that add additional information about the tag without adding extra text to the element. An example will be shown in the chapter, using attributes in an empty element tag.

### Empty elements with attributes in XML document

Elements can have different content types depending on how each element is defined in the XML schema. The different types are element content, mixed content, simple content, and empty content. An XML element consists of everything from the start of the element tag to the close of that element tag.
* An element with element content is the root element - everything in between the opening and closing tags consists of elements only.
| Example: | <tourGuide> |
| | </tourGuide> |
* A mixed content element is one that has text and as well as other elements between its opening and closing tags.
* <restaurant>My favorite restaurant is
* <restaurantName>Provino's Italian Restaurant</restaurantName>
* </restaurant>
* A simple content element is one that contains only text between its opening and closing tags.
| Example: | <restaurantName>Provino's Italian Restaurant</restaurantName> |
* An empty content element, which is an empty element, is one that does not contain anything between its opening and closing tags (or the element tag is opened and ended with a single tag, by using / before the closing of the opening tag.
| Example: | <hotelPicture filename="pan_pacific.jpg" size="80" |
| | value="Image of Pan Pacific"/> |
An empty element is useful when there is no need to specify its content or that the information describing the element is fixed. Two examples illustrated this concept. First, a picture element that references the source of an image with its attributes, but has no need in specifying text content. Second, the owner’s name is fixed for a company, thus it can specify the related information inside the owner tag using attributes. An attribute is meta-information, information that describes the content of the element.

### XML schema data types

Some of the commonly used data types, such as string, decimal, integer, and boolean, are introduced in chapter 2. The following are a few more data types that are useful.
Exhibit 3:Other data types:
| Type | Format | Example | Comment |
| year | YYYY | 1999 | |
| month | YYYY-MM | 1999-03 | Month type is used when the day is irrelevant for the data element |
| time | hh:mm:ss.sss with optional time zone indicator | 20:14:05 | Z for UTC or one of –hh:mm or +hh:mm to indicate the difference from UTC. This time type is used when you want the content to represent a particular time of day that recurs every day, such as 4:15 pm. |
| date | YYYY-MM-DD | 1999-03-14 | |
| anyURI | The domain name specified beginning with http:// | http://www.panpacific.com | |

### More data types

Besides the built-in data types, custom data types can be created as required. A custom data type can be a simple type or complex type. For simplicity, we create a custom data type that is a simple type, which means that the element does not contain other elements or attributes. It contains text only. The creation of a custom simple type starts from using a built-in simple type and applying it with restrictions, or facets, to limit the acceptable values of the tag. A custom simple type can be nameless or named. If the custom simple type is to be used only once, then it makes sense to not name it; thus, that custom type will only be used in where it is defined. Since a named custom type can be referenced (by its name), that custom type can be used wherever necessary.
A pattern can be used to specify exactly how the content of the element should look. For example, one might want to specify the format of a telephone number, a postal code, or a product code. By having a defined pattern for certain elements, the data exchanged will be uniform and the values will be consistent when stored in a database. A useful way to set patterns is through Regex, which will be discussed in later chapters.

### Schema examples

The following is a schema that extends the schema introduced in the previous chapter to include a one-to-many relationship of city to hotels with two examples of custom data types.
Exhibit 1:Data model for 1:m relationship
          1:m relationship - City Hotel
Important, this is a continuing example, so new code is added to the last chapter's example!

#### Inter-document example

Refers to Chapter 2 - A single entity for steps in using NetBeans to create the above XML schema.

## XML document

Attributes
* The valid element naming structure applies to attribute names as well
* In a given element, all attributes’ names must be unique
* An attribute may not contain the symbol ‘<’ The character string ‘&lt;’ can be used to represent it
* Each attribute must have a name and a value. (i.e. <hotelPicture filename=“pan_pacific.jpg” />, filename is the name and pan_pacific.jpg is the value)
* If the assigned value itself contains a quoted string, the type of quotation marks must differ from those used to enclose the entire value. (For instance, if double quotes are used to enclose the whole value then use single quotes for the string: <name familiar=”’Jack’”>John Smith</name>)
Table 3-2: XML Document for a one-to-many relationship – city_hotel.xml
Refers to Chapter 2 - A single entity for steps in using NetBeans to create the above XML document.

## Summary

* Besides the simple built-in data types (e.g, year, month, time, anyURI, and date) schema designers may create custom data types to suit their needs. A simple custom data type can be created from one of the built-in data types by applying to it some restrictions, facets (enumerations that specify a set of acceptable values), or specific patterns.
* An empty element does not contain any text, however, it may contain attributes to provide additional information about that element.
* The presentation layout for displaying a HTML page can include code for style tags, background color, font size, font weight, and alignment. Table tags can be used to organize the layout of content in a HTML page, and images can also be displayed using an image tag.

## Exercises

In order to learn more about the one-to-many relationship, exercises are provided.

## Answers

In order to learn more about the one-to-many relationship, answers are provided to go with the exercises above.