WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 1 (2/4) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 1 (2/4)

To page 1current pageTo page 3To page 4
[previous] [next]

Inside XSLT

Here's an important point to keep in mind: The root node of an XSLT tree represents the entire document. It is not the same as the root element. For example, take a look at the following document; in XSLT terms, the root node represents the whole document, and the root element is <library>:

<?xml version="1.0"?>
<library>
   <book>
       <title>
           Earthquakes for Lunch
       </title>
       <title>
           Volcanoes for Dinner
       </title>
   </book>
</library>

The term root element comes from the XML recommendation, and because it's easy to confuse with the XSLT root node, which comes from the XPath recommendation, some XSLT authors call the root element the document element. This overlap in nomenclature is definitely unfortunate.

In addition, you should know that XSLT processors normalize text nodes. That is, they merge any two adjacent text nodes into one large text node to make it easier to work with the tree structure of a document. This means, for example, that there will never be more than one text node between two adjacent element nodes, as long as there was only text between the element nodes to start with.

In XSLT, nodes can have names, as well as child nodes and parent nodes. In other words, element nodes, attribute nodes, namespace nodes, and processing instruction nodes can have names; every element node and the root node can have children; and all nodes except the root node have parents.

For example, here's how the XML document we just saw looks to an XSLT processor as a tree of nodes:

                          root
                           |
                    element: <library>
                           |
                     element: <book>
                           |
                  |------------------------|
                  |                        |
         element: <title>         element: <title>
                  |                        |
text: "Earthquakes for Lunch"  text: "Volcanoes for Dinner"

As you can see, the root node is at the very top of the tree, followed by the root element's node, corresponding to the <library> element. This is followed by the <book> node, which has two <title> node children. These two <title> nodes are grandchildren of the <library> element. The parents, grandparents, and great-grandparents of a node, all the way back to and including the root node, are that element's ancestors. The nodes that are descended from a node (its children, grandchildren, great-grandchildren, and so on) are called its descendants. Nodes on the same level are called siblings.

This kind of tree model can represent every well-formed XML document. In fact, XSLT is not limited to working with well-formed documents. In well-formed documents, there must be one element that contains all the others, but the XSLT recommendation does not require this. In XSLT, the root node can have any children that an element can have, such as multiple elements or text nodes. In this way, XSLT can work with document fragments, not simply well-formed documents.

Result Tree Fragments
Besides working with input tree fragments, processors can include a special data type in XSLT 1.0 called a result tree fragment in their output. The result tree fragment data type has been eliminated in the XSLT 1.1 working draft, however (see Chapter 7), which means it will probably not be part of XSLT 2.0.

Actually, the tree diagram shown earlier does not represent the whole picture from an XSLT processor's point of view. I've left out one type of node that causes a great deal of confusion: text nodes that contain only whitespace. Because this causes so much confusion in XSLT, it's worth taking a look at now.


To page 1current pageTo page 3To page 4
[previous] [next]

Created: September 12, 2001
Revised: September 12, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/1/2.html