Handling Irregular Data Hierarchies
One of the benefits of using XML is that it allows you to model irregular data hierarchies, including data with the following characteristics.
- Collections of heterogeneous elements
- Structures with many optional elements
- Structures where the order is not apparent
- Recursive structures
- Structures with complex containment requirements
The pole.xml example in this section contains heterogeneous collections of elements, with a complex set of containment requirements. A <section>
element can contain an arbitrary collection of <title>
elements, <p>
elements, <list>
elements, and so on. Many elements are indeed optional: A <section>
does not have to contain <p>
or <list>
elements, or other <section>
elements. It is important to preserve the order of most elements in the output — that is, the first <section>
comes before the second <section>
. The structure is recursive because a <section>
element can contain other <section>
elements. The <emph>
element is allowed anywhere. For example, you might add it to the <title>
element, as follows:
<title>To the Pole... <emph>and Back!</emph></title>
The mechanism for handling data-driven transformations is similar to event-handling in traditional programming languages. Template rules or fragments are analogous to event handlers, which are called whenever the specified events occur. An element matched in the source document triggers a call to the specified template rule or fragment. A template rule or fragment can be called either by its name or according to the matched element type.
The next topic provides a Dissection of the pole.xml Example.