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.
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 Rings']/..
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:
//book[series = 'The Lord of the Rings']
Again, the result is The Two Towers.