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

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:

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.
 * A mixed content element is one that has text and as well as other elements between its opening and closing tags.
 * A simple content element is one that contains only text between its opening and closing tags.
 * 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.

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:

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
 * [[Image:Xmldmcityhotel.png|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 ‘&amp;lt;’ can be used to represent it
 * Each attribute must have a name and a value. (i.e. , 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: John Smith )

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.

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.