Positional Predicates

As well as Boolean expressions, predicates can contain numerical expressions.

foo[5] 
foo[last()] 

These predicates match elements based on their document order in the node set.

Imagine this source document:

<person> 
    <name>Mr. Fish</name> 
    <address>The Sea</address> 
    <appearance>Shiny and scaly</appearance> 
<person> 

This expression produces a node set with three element nodes:

/person/* 

You can use a positional predicate on that node set to pull out the second node within it in document order:

/person/*[2] 

The result will be a node set containing just one node, the element <address>.

A more complex example would be to select the first <img> element in an XHTML document:

//img[1] 

The expression //img matches all

Get Special Edition Using XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.