To understand the operation of XSLT stylesheets on XML data, we are going to have to learn to think a little bit differently than we have up to this point. XSLT defines itself as a series of operations on a tree, which is a representation of an XML document. This tree is an intangible entity that has no defined application programming interface (API); it only describes the objects in the tree, their relationships, and their associated properties. Let's look at a more detailed portion of our XML process diagram and see what this new concept does for us. Figure 2.2. Modifying the tree via XSLT.
So the tree representation of data is com bined in the transform process with the tree of the stylesheet to produce a new tree structure. The new tree can then be output in any of three formats: HTML, text, or XML. This isn't to say that other output formats cannot be generated, but these are the three outlined in the specification. Now that we have the process diagrammed via the tree concept, let's look at an XML document itself in a tree-style format. Conceptually, there's nothing magical about thinking of an XML document as a tree. Listing 2.3 is a portion of our sample XML document. Listing 2.3 A Sample XML Document<PERSON PERSONID="p2"> <NAME> <LAST>Tenney</LAST <FIRST>Corey</FIRST> </NAME> <ADDRESS> <STREET>211 Home Improvement Circle</STREET> <CITY>Roy, UT</CITY> <COUNTRY>USA</COUNTRY> <ZIP>64067</ZIP> </ADDRESS> <TEL/> <EMAIL>tenney@yardwork.com</EMAIL> </PERSON> Now let's look at this document as a tree representation. The ROOT element can be thought of as a representation of the document as a whole. It is the starting point for document processors. The concept of a ROOT element comes from the WC3 XPath specification, which became a Recommendation on November 17, 1999. The specification is available at http://www.w3.org/TR/1999/REC-xpath-19991116. The primary purpose of XPath is to address parts of an XML document. To help accomplish this, it also provides basic methods for manipulation of strings, numbers , and Boolean values. It is important to know that XSLT relies very heavily on XPath. NodesThe entire concept of a document tree comes from XPath's model of an XML document as a tree of nodes. What is a node? In its simplest form, a node is defined as an object in a document tree. Another new object is a context node. This is defined as the node at which you are currently located in the tree during processing. Every time a process is carried out and you move to another node to process it, the context node changes to follow you. Processes are carried out, and relationships between nodes are stated as being in relation to the context node. If this is a little difficult to understand, I'll be showing an example in the next section,"Location Paths." Table 2.2 shows the seven type of nodes contained in a document tree. Table 2.2. XML Document Nodes Types
Armed with these definitions, now I think we can revisit Figure 2.3 and make a lot more sense out of it. Figure 2.3. An XML document tree.
Location PathsThere is one more set of definitions to learn before we dive into the actual structure of stylesheets. XPath introduces, along with other concepts that will be covered at various points in this chapter, the model of self, children, descendents, siblings, and ancestors . XPath uses these terms to describe the concept of location paths, or to put it succinctly, a means of navigation around an XML tree. Table 2.3 defines the location path entities. Table 2.3. XPath Location Path Entities
Remember the context node concept that I brought up in the earlier section "Nodes"? Well, these new terms like "children" and so on are defined in terms of a context node. Examples and syntax for these location paths will be discussed in the section "Patterns (Abbreviated Syntax)" later in this chapter. For now, though, I want you to see the interrelationships of these entities, and we'll do this utilizing our XML document tree in Figure 2.3 in concert with our sample document in Listing 2.3. Let's look at Table 2.4 and the element relationships expressed there, using keywords from XPath. Table 2.4. XPath Location Path Entity Relationships
Did the entities "preceding" and "following," along with their sibling counterparts, throw you? Remember the definitions in Table 2.3. They are defined in terms of document order. If the tree diagram is not drawn exactly according to the document order, it can lead you to incorrect results. Be sure the diagram is correct before you use it to navigate a document. |