Chapter 5 - XPath Espresso | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
People using XSLT have their own dance called the Location Step. Ready? 1-2-3 1-2-3 Well, even if you dont have happy feet, you can learn how to do this boogie. A location step is the basic unit that you use when creating XPath expressions and is central to everything done with XSLT. Each location step consists of an axis, a node test, and an optional predicate:
A location step takes the following form: axis::nodetest[predicate] . Figure 5-1 shows the XPath triumvirate. Figure 5-1: Parts of a location step. Tip In earlier chapters, I have used the term current node (also commonly referred to as the context node ) to describe the node that the XSLT processor is on during its traversal of the document tree. Although the term current node is accurate, it is also potentially confusing when you work with location steps, because the current node is not necessarily the selected node. The current node is a temporary home base that the XPath commandos start from to return a selection of nodes. On occasion, the location step returns the current node itself, but more often, it returns nodes that are only related in some way to the current node. Steppin through a funnelYou can think of a location step as a sifter (see Figure 5-2). For each source tree node encountered , the XSLT processor runs it through a location step. Starting at the topmost part of the sifter, the processor selects only those nodes that meet the axis specifications, throwing all others out before getting to the node test. In the middle section of the sifter, this node set is then further qualified against the criteria set by the node test. For all the remaining nodes of the set, they go through one final screen at the bottom of the sifter if a predicate is defined. What comes out at the bottom of the sifter is the end result of the location step. Figure 5-2: Location step as a funnel. For example, consider the following location step: child::book[@id] When a source tree is processed, for each node, the XSLT processor first uses the child:: axis and returns all the current nodes children as a node set. Next, the processor uses the book node test and returns just the child nodes that are book element nodes. Finally, from this smaller node set, it then uses the @id predicate to filter out all book elements that do not have an id attribute defined. Steppin up to a location pathAs I mentioned earlier in this chapter, a location path consists of one or more location steps. If more than one location step is specified, then a / is used to separate each step. For example, part/chapter is a location path with two location steps ( part and chapter ). These two steps form a sequence that is examined from left to right, indicating that the second step is a child of the first. So, in this example, the chapter element is a child of the part element.
|