|
XPath is an expression language for finding selected entities within an XML document. The following shows some simple XPath constructs. The Liquid XPath Expression Builder makes it simple to visualize the results of your XPath Expressions.
Sample XML Document
1 <?xml version="1.0" encoding="utf-8"?>
2 <!-- Created with Liquid XML Studio 0.9.13.0 (http://www.liquid-technologies.com) -->
3 <bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:noNamespaceSchemaLocation="BookStore.xsd">
5 <book price="8.99" publicationdate="1981-05-11" ISBN="1-861003-11-0">
6 <title>The Autobiography of Benjamin Franklin</title>
7 <author>
8 <first-name>Benjamin</first-name>
9 <last-name>Franklin</last-name>
10 </author>
11 <genre>autobiography</genre>
12 </book>
13 <book price="16.95" publicationdate="1995-06-08" ISBN="978-0091779092 ">
14 <title>Fly Fishing</title>
15 <author>
16 <first-name>J.R</first-name>
17 <last-name>Hartley</last-name>
18 </author>
19 <genre>Reference</genre>
20 </book>
21 <book price="11.64" publicationdate="2007-05-03" ISBN="978-0241141847 ">
22 <title>Wildwood: A Journey Through Trees</title>
23 <author>
24 <first-name>Roger</first-name>
25 <last-name>Deakin</last-name>
26 </author>
27 <genre>Ecology</genre>
28 </book>
29 <audioBook price="25.68" publicationdate="1982-05-03" ISBN="978-75648984">
30 <title>The Hitchhikers Guide</title>
31 <author>
32 <first-name>Douglas</first-name>
33 <last-name>Adams</last-name>
34 </author>
35 <format>Tape</format>
36 <genre>Scifi</genre>
37 </audioBook>
38 </bookstore>
|
XPath Examples (using the XML above)
| XPath Example |
Description of results |
| /bookstore/book |
The 3 <book> elements |
| /bookstore/book[2] |
The second <book> element (Fly Fishing) |
| /bookstore/book[2]/title |
The title element from the <book> Fly Fishing |
| /bookstore/book[2]/title/text() |
Just the text contained within the title element from the <book> Fly Fishing (i.e. "Fly Fishing") |
| /bookstore/book/title |
The <title> element for all 3 books |
| /bookstore/book/@price |
The price attribute for all 3 books |
| /bookstore/book[@price="8.99"] |
The <book> element where the price = 8.99 |
| /bookstore/book[@price>"10"] |
All the <book> elements where the price attribute is greater than 10 |
| /bookstore/book[@price>"10"]/title |
The <title> element of any <book> with a price greater than 10 |
| /bookstore/book[author/first-name="Benjamin"] |
The <book> element where the authors first name is "Benjamin" |
| //title |
All the <title> elements in the document (regardless of there parent or position). So
<title>The Autobiography of Benjamin Franklin</title>
<title>Fly Fishing</title>
<title>Wildwood: A Journey Through Trees</title>
<title>The Hitchhikers Guide</title> |
| /bookstore/* |
All the child elements of root element <bookstore>, i.e. the 3 <book> elements and the 1 <audioBook> element. |
| /bookstore/*/title |
The <title> element of all the books and audioBooks in the file (safer than using //title, as this matches any title element regardless of its position). |
| /bookstore/*[@price>"10"]/title |
All books/audiobooks with a price greater than 10 |
| /bookstore/audioBook/@* |
All the attributes contained in all the audioBook elements. |
|