Java Reference
In-Depth Information
//Get the penultimate (one before the last) book that is a 'classic'
//whose price is between $5 and $10.
xpath = "//book[category='classics' and (price > 5 and price <
10)][last()-1]";
//Prints the entire King Lear node
//Get the title node with the value of Hamlet
xpath = "//title[.='Hamlet']";
//Get the title of the first book whose author starts with 'William'
xpath = "//book[1][author[starts-with(., 'William')]]/title";
//Prints: Value=King Lear
//Gets the categories of the topics after the 2nd one in the tree
xpath = "//book[position() > 2]/category";
//Gets any authors that are co-authors,
//that is, all <author> nodes under an <authors> node
xpath = "//authors//author";
//Prints:
Value=Maurice Naftalin
Value=Phillip Wadler
As you can see, operator precedence is specified with parentheses. While a complete discus-
sion of XPath is beyond the scope of this topic (and beyond what you'll need 90% of the time),
you can read the complete specification at http://www.w3.org/TR/xpath , or just look there for
more string functions.
Search WWH ::




Custom Search