Showing posts with label Xpath. Show all posts
Showing posts with label Xpath. Show all posts

Thursday 20 March 2014

Xpath Expression Examples

Xpath Expression Examples

Xpath: It is syntax, used to navigate the elements and their attributes. Its make easy to get the any element or attribute.

XPath Terminology
1. element
2. attribute
3. text
4. namespace
5. processing-instruction
6. comment
7. document nodes

Following are few Xpath Expression and their Description
Xpath Expression Description
./writer All <writer> elements within the current context. Note that this is equivalent to the expression in the next row.
writer All <writer> elements within the current context.
first.name All <first.name> elements within the current context.
/bookStore The document element (<bookStore>) of this document.
//writer All <writer> elements in the document.
book[/bookStore/@specialty=@style] All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookStore> element at the root of the document.
writer/first-name All <first-name> elements that are children of an <writer> element.
bookStore//title All <title> elements one or more levels deep in the <bookStore> element (arbitrary descendants). Note that this is different from the expression in the next row.
bookStore/*/title All <title> elements that are grandchildren of <bookStore> elements.
bookStore//book/excerpt//emph All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookStore> element.
.//title All <title> elements 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.
writer/* All elements that are the children of <writer> elements.
book/*/lastName All <lastName> elements that are grandchildren of <book> elements.
*/* All grandchildren elements of the current context.
*[@specialty] All elements with the specialty attribute.
@style The style attribute of the current context.
price/@exchange The exchange attribute on <price> elements within the current context.
price/@exchange/total Returns an empty node set, because attributes do not contain element children. This expression is allowed by the XML Path Language (XPath) grammar, but is not strictly valid.
book[@style] All <book> elements with style attributes, of the current context.
book/@style The style attribute for all <book> elements of the current context.
@* All attributes of the current element context.
./first-name All <first-name> elements in the current context node. Note that this is equivalent to the expression in the next row.
first-name All <first-name> elements in the current context node.
writer[1] The first <writer> element in the current context node.
writer[first-name][3] The third <writer> element that has a <first-name> child.
my:book The <book> element from the my namespace.
my:* All elements from the my namespace.
@my:* All attributes from the my namespace (this does not include unqualified attributes on elements from the my namespace).
Note that indexes are relative to the parent. Consider the following data:

 

Xpath Expression Description
x/y[1] The first <y> child of each <x>. This is equivalent to the expression in the next row.
x/y[position() = 1] The first <y> child of each <x>.
(x/y)[1] The first <y> from the entire set of <y> children of <x> elements.
x[1]/y[2] The second <y> child of the first <x>.
 

 

Xpath Expression Description
book[last()] The last <book> element of the current context node.
book/writer[last()] The last <writer> child of each <book> element of the current context node.
(book/writer)[last()] The last <writer> element from the entire set of <writer> children of <book> elements of the current context node.
book[excerpt] All <book> elements that contain at least one <excerpt> element child.
book[excerpt]/title All <title> elements that are children of <book> elements that also contain at least one <excerpt> element child.
book[excerpt]/writer[degree] All <writer> elements that contain at least one <degree> element child, and that are children of <book> elements that also contain at least one <excerpt> element.
book[writer/degree] All <book> elements that contain <writer> children that in turn contain at least one <degree> child.
writer[degree][award] All <writer> elements that contain at least one <degree> element child and at least one <award> element child.
writer[degree and award] All <writer> elements that contain at least one <degree> element child and at least one <award> element child.
writer[(degree or award) and publication] All <writer> elements that contain at least one <degree> or <award> and at least one <publication> as the children
writer[degree and not(publication)] All <writer> elements that contain at least one <degree> element child and that contain no <publication> element children.
writer[not(degree or award) and publication] All <writer> elements that contain at least one <publication> element child and contain neither <degree> nor <award> element children.
writer[lastName = "Bob"] All <writer> elements that contain at least one <lastName> element child with the value Bob.
writer[lastName[1] = "Bob"] All <writer> elements where the first <lastName> child element has the value Bob. Note that this is equivalent to the expression in the next row.
writer[lastName [position()=1]= "Bob"] All <writer> elements where the first <lastName> child element has the value Bob.
degree[@from != "Harvard"] All <degree> elements where the from attribute is not equal to "Harvard".
writer[. = "Matthew Bob"] All <writer> elements whose value is Matthew Bob.
writer[lastName = "Bob" and ../price &gt; 50] All <writer> elements that contain a <lastName> child element whose value is Bob, and a <price> sibling element whose value is greater than 50.
book[position() &lt;= 3] The first three books (1, 2, 3).
writer[not(lastName = "Bob")] All <writer> elements that do no contain <lastName> child elements with the value Bob.
writer[first-name = "Bob"] All <writer> elements that have at least one <first-name> child with the value Bob.
writer[* = "Bob"] all writer elements containing any child element whose value is Bob.
writer[lastName = "Bob" and first-name = "Joe"] All <writer> elements that has a <lastName> child element with the value Bob and a <first-name> child element with the value Joe.
price[@intl = "Canada"] All <price> elements in the context node which have an intl attribute equal to "Canada".
degree[position() &lt; 3] The first two <degree> elements that are children of the context node.
p/text()[2] The second text node in each <p> element in the context node.
ancestor::book[1] The nearest <book> ancestor of the context node.
ancestor::book[writer][1] The nearest <book> ancestor of the context node and this <book> element has an <writer> element as its child.
ancestor::writer[parent::book][1] The nearest <writer> ancestor in the current context and this <writer> element is a child of a <book> element.