1.6. Climbing 'Round the Family Tree: Addressing in XSLT

Navigation in XSLT and XPath involves addressing the various nodes with respect to their relationship with one another. If you put “tree” and “relationships” together, a logical inference is to use a tree analogy to model how members of a family are related. So, with XPath, the terminology for how one node is positioned in an XML document instance with respect to another is done in terms of family terminology, or a family tree.

A family tree traces one's parents, grandparents, and other ancestors. XML uses the same familial terminology to describe the relationships between the nodes of a document.

Up to this point, we have used tree representations to show the structure of elements in an XML document. Now we are going to show nesting using a different form of representation, using boxes to show nesting. In Figure 1-8, consider the representation of XML and the full concept of element nesting as it is presented. In this way, the logical structure of a simple XML document instance can be represented.

Figure 1-8. Nesting of XML markup using boxes.


This example shows the document element, <year>, which contains (as a boxed set) the <planting> and <harvest> elements, and so on. The same logical structure can be represented in the tried and true tree paradigm, as shown in Figure 1-9.

Figure 1-9. Nesting of XML markup using trees.


In any family tree, the oldest traceable ancestor is always at the top. In this case, <year> is the ancestor of all the other nodes in the instance. In addition, we can say that <year> is the parent of both <planting> and <harvest>. Accordingly, then, <planting> and <harvest> are both children of <year>. If we asked for the parent of <harvest>, we would get <year>; for the parent of <planting>, we would also get <year>. This means that <planting> and <harvest> can also be called siblings to one another.

As we go further down the tree, additional features of familial relationships come into play. The various <season> nodes are all descendants of <year>, and <year> is also their shared, or common, ancestor.

In this kind of terminology that is so crucial for XPath, we do not say “grandparent” or “great-grandparent.” Any predecessor in the element hierarchy of the logical structure that is more than one node level removed is an ancestor.[6]

[6] This is correct with common practice, as a grandparent is an ancestor as well as a grandparent, and is more an ancestor than a parent with respect to the direct progenitor relationship.

Lines of descendancy are kept carefully intact in XML and XPath, just as they usually are in the human realm. Where <harvest> and <planting> are siblings, so also are both of them parents to their own respective sets of <season> elements. The two <season> elements with attribute values for period of fall and winter, are both siblings because they share the same parent, <harvest>. Similarly, the <season> elements with the period attribute values spring and summer are also siblings sharing the same parent, <planting>.

The summer <season>, then, is not a sibling to the winter <season>. They do not share the same parent. In life, we might call these cousins, but neither XPath nor XML standards as a whole use this terminology.[7] Of course, every <season> is at the same logical level in this document's hierarchy, but they are not siblings.

[7] This is partly because “cousin,” among other things, can have many different familial significations in English-speaking cultures and so becomes quite problematic as an intuitive referent of a relationship.

As a definition of node relationships, each <month> within the fall <season> is:

  • A sibling of the other <month> elements in the same <season>

  • A child of its parent <season period="fall">

  • A descendant of <harvest>

  • A descendant of <year>

  • A descendant of the document root

  • The parent of the text node it contains

Another thing that might not be apparent at first glance, but is an unspoken assumption in working with the familial terminology in the tree structure, is that each of the families represented in the node branch are single-parent families. Call it a reflection of the post-modern world, but it is an important distinction of XML that no node can have more than one parent. Any node (except the root node) can have many ancestors, but only one parent.

Another point to mention is the relationship between attributes and elements. An element node such as <season> is called the parent of the attribute node with the attribute name period. However, the attribute period is not considered the child of <season>.

With this basic set of terms in mind, you can point to any part of a document based on its logical structure. This makes the value of markup and the logical structure it provides quite apparent, if, for example, the markup was in another language. In the case of our year example, providing the weather in the hemisphere from which that language arose was compatible, it would be possible to refer to the same period without knowing the specific word for that month. XPath relies heavily on this familial structure to navigate through a document when processing a particular XSLT function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset