Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
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.
Copy Code | |
---|---|
./author |
Note that this is equivalent to the following.
Copy Code | |
---|---|
author |
Find all first.name elements.
Copy Code | |
---|---|
first.name |
Find the document element (bookstore) of this document.
Copy Code | |
---|---|
/bookstore |
Find all author elements that are children of the current context node (for example, /bookstore/book).
Copy Code | |
---|---|
//author |
Find all bookstores where the value of the specialtyattribute is equal to "textbooks".
Copy Code | |
---|---|
/bookstore[@specialty = "textbooks"] |
Find all books where the value of the styleattribute on the book is equal to the value of the specialtyattribute of the bookstore element at the root of the document.
Copy Code | |
---|---|
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.
Copy Code | |
---|---|
author/first-name |
Find all title elements one or more levels deep in the bookstore (arbitrary descendants).
Copy Code | |
---|---|
bookstore//title |
Note that this is different from the following pattern, which finds all title elements that are grandchildren of bookstore elements.
Copy Code | |
---|---|
bookstore/*/title |
Find emph elements anywhere inside book excerpts, anywhere inside the bookstore.
Copy Code | |
---|---|
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.
Copy Code | |
---|---|
.//title |
Find all element children of author elements.
Copy Code | |
---|---|
author/* |
Find all last names that are grandchildren of books.
Copy Code | |
---|---|
book/*/last-name |
Find the grandchildren elements of the current context.
Copy Code | |
---|---|
*/* |
Find the book element from the "my" name space.
Copy Code | |
---|---|
my:book |
Find all elements from the "my" name space.
Copy Code | |
---|---|
my:* |
Find all elements with the specialtyattribute.
Copy Code | |
---|---|
*[@specialty] |
Find the styleattribute of the current element context.
Copy Code | |
---|---|
@style |
Find the exchangeattribute on price elements within the current context.
Copy Code | |
---|---|
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.
Copy Code | |
---|---|
price/@exchange/total |
Find all books with styleattributes.
Copy Code | |
---|---|
book[@style] |
Find the styleattribute for all book elements.
Copy Code | |
---|---|
book/@style |
Find all attributes of the current element context.
Copy Code | |
---|---|
@* |
Find all attributes from the "my" name space. This does not include unqualified attributes on elements from the "my" name space.
Copy Code | |
---|---|
@my:* |
Find all first-name elements. The following examples are equivalent.
Copy Code | |
---|---|
./first-name first-name |
Find all unqualified book elements.
Copy Code | |
---|---|
book |
For example, the following finds the first author element.
Copy Code | |
---|---|
author[1] |
The following finds the third author element that has a first-name.
Copy Code | |
---|---|
author[first-name][3] |
Note that indexes are relative to the parent. Consider the following data.
Copy Code | |
---|---|
<x> <y/> <y/> </x> <x> <y/> <y/> </x> |
Find the first y from each x.
Copy Code | |
---|---|
x/y[1] x/y[position() = 1] |
Find the first y from the entire set of y elements within x elements.
Copy Code | |
---|---|
(x/y)[1] |
Find the second y from the first x.
Copy Code | |
---|---|
x[1]/y[2] |
Find the last book.
Copy Code | |
---|---|
book[last()] |
Find the last author for each book.
Copy Code | |
---|---|
book/author[last()] |
Find the last author from the entire set of authors of books.
Copy Code | |
---|---|
(book/author)[last()] |
Find all books that contain at least one excerpt element.
Copy Code | |
---|---|
book[excerpt] |
Find all titles of books that contain at least one excerpt element.
Copy Code | |
---|---|
book[excerpt]/title |
Find all authors of books where the book contains at least one excerpt and the author has at least one degree.
Copy Code | |
---|---|
book[excerpt]/author[degree] |
Find all books that have authors with at least one degree.
Copy Code | |
---|---|
book[author/degree] |
Find all books that have an excerpt and a title.
Copy Code | |
---|---|
book[excerpt][title] |
Find all author elements that contain at least one degree and one award.
Copy Code | |
---|---|
author[degree and award] |
Find all author elements that contain at least one degree or award and at least one publication.
Copy Code | |
---|---|
author[(degree or award) and publication] |
Find all author elements that contain at least one degree element and that contain no publication elements.
Copy Code | |
---|---|
author[degree and not(publication)] |
Find all author elements that contain publication elements but do not contain either degree elements or award elements.
Copy Code | |
---|---|
author[not(degree or award) and publication] |
Find all author elements that contain a last-name element with the value Bob.
Copy Code | |
---|---|
author[last-name = "Bob"] |
Find all author elements where the first last-name is Bob.
Copy Code | |
---|---|
author[last-name[1] = "Bob"] author[last-name = "Bob"] |
Find all authors where the fromattribute is not equal to "Harvard".
Copy Code | |
---|---|
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.)
Copy Code | |
---|---|
author[last-name = /guest/last-name] |
Find all authors whose text is "Matthew Bob".
Copy Code | |
---|---|
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.)
Copy Code | |
---|---|
author[last-name = "Bob" and price > 50] |
Find all authors whose last name begins with "M" or greater.
Copy Code | |
---|---|
author[last-name >= "M"] |
When an author can have several last names in the schema (for example, Clemens and Twain), use the following patterns.
Copy Code | |
---|---|
author[all last-name >= "M"] |
Find the first three books (1, 2, 3).
Copy Code | |
---|---|
book[position() <= 3] |
Find all author elements where any one of the last names is Bob.
Copy Code | |
---|---|
author[last-name = "Bob"] |
Find all author elements where none of the last-name elements is Bob.
Copy Code | |
---|---|
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.)
Copy Code | |
---|---|
author[first-name = "Bob"] |
Find all author elements containing any child element whose text is "Bob".
Copy Code | |
---|---|
author[* = "Bob"] |
Find author elements equal to "Joe Bob".
Copy Code | |
---|---|
author[last-name = "Bob" and first-name = "Joe"] |
Find the intlattribute equal to "Canada".
Copy Code | |
---|---|
price[@intl = "Canada"] |
Find the first 3 degrees.
Copy Code | |
---|---|
degree[position() < 3] |
Find the second text node in each p element in the current context.
Copy Code | |
---|---|
p/text()[2] |
Find the nearest book ancestor of the current element.
Copy Code | |
---|---|
ancestor::book[1] |
Find the nearest ancestor author element that is contained in a book element.
Copy Code | |
---|---|
ancestor::book[author][1] |
To illustrate unions, consider the following data.
Copy Code | |
---|---|
<root> <x/> <!-- green --> <y> <x/> <!-- blue --> <x/> <!-- blue --> </y> <z> <x/> <x/> </z> <x/> <!-- green --> </root> |
Find both green and blue nodes.
Copy Code | |
---|---|
x | y/x |