Helpful XPath examples
Predicates
/bookstore/book[1] # Selects the first book element that is the child of the bookstore element. # Note: IE5 and later has implemented that [0] should be the first node, # but according to the W3C standard it should have been [1]!! /bookstore/book[last()] # Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1] # Selects the last but one book element that is the child of the bookstore element /bookstore/book[position()<3] # Selects the first two book elements that are children of the bookstore element //title[@lang] # Selects all the title elements that have an attribute named lang
Selecting Unknown Nodes
/bookstore/* # Selects all the child nodes of the bookstore element //* # Selects all elements in the document //title[@*] # Selects all title elements which have any attribute
Selecting Several Paths
//book/title | //book/price # Selects all the title AND price elements of all book elements