JavaScript Editor Free JavaScript Editor     JavaScript Debugger 

Main Page

Previous Page
Next Page

9.5. Predicates

Predicates are the equivalent to an SQL WHERE clause, basically a way to limit the node set returned by XPath. The basic format is as follows:


Although this isn't very difficult, most mistakes are made in the condition. This is because there is a difference between evaluating XPath in Altova's XMLSPY XPath Evaluator and evaluating XPath in XSLT. I'll give you a hint: "well formed". XMLSPY XPath Evaluator uses the standard programming greater than (>) and less than (<) conditional operators. In XSLT, this would result in the document being not well formed. Table 9-1 lists the conditional operators used in both.

Table 9-1. Conditional Operators Used in XMLSPY XPath Evaluator and XPath in XSLT






Greater than



Less than



Equal to



Not equal to

Using the XPath Evaluator, the XPath statement to return all the books published by Del Rey would be as follows:

//book[@publisher = 'Del Rey']

This statement results in a node set of five books: one by Simak and four by Tolkien. But what if we want only the books that are not part of the Lord of the Rings trilogy? In SQL, we use an "and" condition. Because XPath supports both "and" and "or," we do the same:

//book[@publisher = 'Del Rey' and series != 'The Lord of the Rings']

This results in a single XML book node, Simak's Way Station. An alternate, although more verbose, way of coding to obtain the same result shows that multiple predicates can be on a single XPath statement:

//book[@publisher = 'Del Rey']/series[. != 'The Lord of the

In addition to being able to obtain nodes and node sets based upon Boolean conditions, it is possible to retrieve a particular instance of a node. For example, let's say that we want the third book in the library, The Two Towers. The easiest method of getting it is this:


This method also can be combined with a Boolean condition to obtain the name of the second book in Tolkien's trilogy:

//book[series = 'The Lord of the Rings'][2]

Again, the result is The Two Towers.

Previous Page
Next Page

JavaScript Editor Free JavaScript Editor     JavaScript Debugger