Flylib.com

Books Software

 
 
 

XPath, The X-Teams Commando

 
xslt for dummies
Chapter 5 - XPath Espresso
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

XPath, The X-Teams Commando

XPath is the language used by XSLT to describe how to locate nodes in a source XML document. Think of XPath as the spy or commando who is charged with going into foreign soil, picking out the requested information, and returning it to the XSLT homeland. Every template rule contains one of these XPath commando units that has a mission something like: Go into the source tree and traverse each node. As you do so, look for a state parent that has city children. Bring each of the nodes that match back to me in this handy sack for further processing."

Unlike most languages, XPath has a vocabulary that is extremely targeted . Be it English, French, or Russian, any natural language spoken in the world has a rich, verbose vocabulary. Programming languages, like C or Java, are much more constrained, but you still can use these languages to create programs that have thousands or even millions of lines of code. In contrast, XPaths expressions are almost always placed on a single line.

 Technical Stuff   XPath was originally part of the W3Cs XSLT specification. But after the W3C working group discovered that other non-XSLT specifications, such as XPointer, can use XPath, the group pulled XPath out of the XSLT specification and standardized it on its own.

The primary use of XPath is to create location paths, which are instructions that specify what nodes to bring back to the template rule. More precisely, a location path is an XPath expression that is made up of a series of steps called location steps. A location paths return value is always a node set.

  
 
 
2000-2002    Feedback
 
xslt for dummies
Chapter 5 - XPath Espresso
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Dancing the Location Step

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:

  • The axis spells out the relationship of the nodes to be selected to the current node being processed .

  • The node test identifies the particular nodes that the axis selected and that meet certain conditions.

  • A predicate is an optional filter that sifts out some of the nodes returned by the axis and node test parts .

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 funnel

You 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 path

As 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.

  
 
 
2000-2002    Feedback