by Vahid
7. May 2009 07:00
if you work with XML and xpath for sure the reference seems to be a trivial one for you. but if you stop working with xml and xpath for a while you’ll find the reference pretty handy. i personally keep it with me always. hope it also helps you.
the reference xpath expression is for this xml content:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <Albums>
3: <Album country="USA">
4: <title>Empire Burlesque</title>
5: <artist>Bob Dylan</artist>
6: <price>10.90</price>
7: </Album>
8: <Album country="UK">
9: <title>Hide your heart</title>
10: <artist>Bonnie Tyler</artist>
11: <price>10.0</price>
12: </Album>
13: <Album country="USA">
14: <title>Greatest Hits</title>
15: <artist>Dolly Parton</artist>
16: <price>9.90</price>
17: </Album>
18: </Albums>
and here is the quick reference
/Albums
|
selects the root element
|
/Albums/Album
|
selects all the Album elements of the Albums element
|
/Albums/Album/price
|
selects all the price elements of all the Album elements of the Albums element
|
/Albums/Album[price>10.0]
|
selects all the Album elements with price greater than 10.0
|
starts with a slash(/)
|
represents an absolute path to an element
|
starts with two slashes(//)
|
selects all elements that satisfy the criteria
|
//Album
|
selects all Album elements in the document
|
/Albums/Album/title | /Albums/Album/artist
|
selects all the title and artist elements of the Album elements of Albums
|
//title | //artist
|
selects all the title and artist elements in the document
|
/Albums/Album/*
|
selects all the child elements of all Album elements of the Albums element
|
/Albums/*/price
|
selects all the price elements that are grandchildren of Albums
|
/*/*/price
|
selects all price elements which have two ancestors
|
//*
|
selects all elements in the document
|
/Albums/Album[1]
|
selects the first Album child of Albums
|
/Albums/Album[last()]
|
selects the last Album child of Albums
|
/Albums/Album[price]
|
selects all the Album elements that have price
|
/Albums/Album[price=10.90]
|
selects Album elements with the price of 10.90
|
/Albums/Album[price=10.90]/price
|
selects all price elements with the price of 10.90
|
//@country
|
selects all "country" attributes
|
//Album[@country]
|
selects Album elements which have a "country" attribute
|
//Album[@*]
|
selects Album elements which have any attribute
|
//Album[@country='UK']
|
selects Album elements with "country" attribute equal to 'UK'
|