WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 1 (1/4)
Trees and Nodes
When you're working with XSLT, you no longer think in terms of documents, but rather in terms of trees. A tree represents the data in a document as a set of nodes-elements, attributes, comments, and so on are all treated as nodes-in a hierarchy, and in XSLT, the tree structure follows the W3C XPath recommendation (www.w3.org/TR/xpath). In this chapter, I'll go through what's happening conceptually with trees and nodes, and in Chapters 3 and 4, I'll give a formal introduction to XPath and how it relates to XSLT. You use XPath expressions to locate data in XML documents, and those expressions are written in terms of trees and nodes.
In fact, the XSLT recommendation does not require conforming XSLT processors to have anything to do with documents; formally, XSLT transformations accept a source tree as input, and produce a result tree as output. Most XSLT processors do, however, add support so that you can work with documents.
From the XSLT point of view, then, documents are trees built of nodes; XSLT recognizes seven types of nodes:
- The root node. This is the very start of the document. This node represents the entire document to the XSLT processor. Important: Don't get the root node mixed up with the root element, which is also called the document element (more on this later in this chapter).
- Attribute node. Holds the value of an attribute after entity references have been expanded and surrounding whitespace has been trimmed.
- Comment node. Holds the text of a comment, not including
- Element node. Consists of the part of the document bounded by a start and matching
end tag, or a single empty element tag, such as
- Namespace node. Represents a namespace declaration-and note that it is added to each element to which it applies.
- Processing instruction node. Holds the text of the processing instruction, which does
?>. The XML declaration,
<?xml version="1.0"?>, by the way, is not a processing instruction, even though it looks like one. XSLT processors strip it out automatically.
- Text node. Text nodes hold sequences of characters-that is, PCDATA text. Text nodes are normalized by default in XSLT, which means that adjacent text nodes are merged.
As you'll see in Chapter 7, you use XPath expressions to work with trees and nodes. An XPath expression returns a single node that matches the expression; or, if more than one node matches the expression, the expression returns a node set. XPath was designed to enable you to navigate through trees, and understanding XPath is a large part of understanding XSLT.
Created: September 12, 2001
Revised: September 12, 2001