XPath Examples (Windows CE 5.0)
This topic reviews the examples that appear throughout this documentation. All are based on the Sample Data.
Find all author elements within the current context.
./author
Note that this is equivalent to the following.
author
Find all first.name elements.
first.name
Find the document element (bookstore) of this document.
/bookstore
Find all author elements that are children of the current context node (for example, /bookstore/book).
//author
Find all bookstores where the value of the specialty attribute is equal to "textbooks".
/bookstore[@specialty = "textbooks"]
Find all books where the value of the style attribute on the book is equal to the value of the specialty attribute of the bookstore element at the root of the document.
book[/bookstore/@specialty = @style]
Find all first-name elements within an author element. Note that the author children of the current context are found, and then first-name children are found relative to the context of the author elements.
author/first-name
Find all title elements one or more levels deep in the bookstore (arbitrary descendants).
bookstore//title
Note that this is different from the following pattern, which finds all title elements that are grandchildren of bookstore elements.
bookstore/*/title
Find emph elements anywhere inside book excerpts, anywhere inside the bookstore.
bookstore//book/excerpt//emph
Find all titles one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required.
.//title
Find all element children of author elements.
author/*
Find all last names that are grandchildren of books.
book/*/last-name
Find the grandchildren elements of the current context.
*/*
Find the book element from the "my" name space.
my:book
Find all elements from the "my" name space.
my:*
Find all elements with the specialty attribute.
*[@specialty]
Find the style attribute of the current element context.
@style
Find the exchange attribute on price elements within the current context.
price/@exchange
The following example does not return anything because attributes do not contain element children. It is allowed by the XML Path Language (XPath) grammar, but is not strictly valid.
price/@exchange/total
Find all books with style attributes.
book[@style]
Find the style attribute for all book elements.
book/@style
Find all attributes of the current element context.
@*
Find all attributes from the "my" name space. This does not include unqualified attributes on elements from the "my" name space.
@my:*
Find all first-name elements. The following examples are equivalent.
./first-name
first-name
Find all unqualified book elements.
book
For example, the following finds the first author element.
author[1]
The following finds the third author element that has a first-name.
author[first-name][3]
Note that indexes are relative to the parent. Consider the following data.
<x>
<y/>
<y/>
</x>
<x>
<y/>
<y/>
</x>
Find the first y from each x.
x/y[1]
x/y[position() = 1]
Find the first y from the entire set of y elements within x elements.
(x/y)[1]
Find the second y from the first x.
x[1]/y[2]
Find the last book.
book[last()]
Find the last author for each book.
book/author[last()]
Find the last author from the entire set of authors of books.
(book/author)[last()]
Find all books that contain at least one excerpt element.
book[excerpt]
Find all titles of books that contain at least one excerpt element.
book[excerpt]/title
Find all authors of books where the book contains at least one excerpt and the author has at least one degree.
book[excerpt]/author[degree]
Find all books that have authors with at least one degree.
book[author/degree]
Find all books that have an excerpt and a title.
book[excerpt][title]
Find all author elements that contain at least one degree and one award.
author[degree and award]
Find all author elements that contain at least one degree or award and at least one publication.
author[(degree or award) and publication]
Find all author elements that contain at least one degree element and that contain no publication elements.
author[degree and not(publication)]
Find all author elements that contain publication elements but do not contain either degree elements or award elements.
author[not(degree or award) and publication]
Find all author elements that contain a last-name element with the value Bob.
author[last-name = "Bob"]
Find all author elements where the first last-name is Bob.
author[last-name[1] = "Bob"]
author[last-name = "Bob"]
Find all authors where the from attribute is not equal to "Harvard".
degree[@from != "Harvard"]
Find all authors where the last name is the same as the /guest/last-name element. (This assumes there is only one last-name. See Set Operations.)
author[last-name = /guest/last-name]
Find all authors whose text is "Matthew Bob".
author[. = "Matthew Bob"]
Find all author elements whose last name is "Bob" and whose price is > 50. (This assumes there is only one last-name and price for an author. See Set Operations.)
author[last-name = "Bob" and price > 50]
Find all authors whose last name begins with "M" or greater.
author[last-name >= "M"]
When an author can have several last names in the schema (for example, Clemens and Twain), use the following patterns.
author[all last-name >= "M"]
Find the first three books (1, 2, 3).
book[position() <= 3]
Find all author elements where any one of the last names is Bob.
author[last-name = "Bob"]
Find all author elements where none of the last-name elements is Bob.
author[not(last-name = "Bob")]
Find all author elements containing a first-name child whose text is "Bob". (This and the following examples assume there is only one first-name child for an author.)
author[first-name = "Bob"]
Find all author elements containing any child element whose text is "Bob".
author[* = "Bob"]
Find author elements equal to "Joe Bob".
author[last-name = "Bob" and first-name = "Joe"]
Find the intl attribute equal to "Canada".
price[@intl = "Canada"]
Find the first 3 degrees.
degree[position() < 3]
Find the second text node in each p element in the current context.
p/text()[2]
Find the nearest book ancestor of the current element.
ancestor::book[1]
Find the nearest ancestor author element that is contained in a book element.
ancestor::book[author][1]
To illustrate unions, consider the following data.
<root>
<x/> <!-- green -->
<y>
<x/> <!-- blue -->
<x/> <!-- blue -->
</y>
<z>
<x/>
<x/>
</z>
<x/> <!-- green -->
</root>
Find both green and blue nodes.
x | y/x
See Also
Sample Data | XQL Application Development
Send Feedback on this topic to the authors