node1/.
*** selects contex node (whole node itself) after node1
node1/*
*** selects only subnodes of node1
/comment()
selects all comment nodes which are children of the document root
/company/office
selects all 'office' elements which are children of the 'company' element
/company/office[2]
'[2]' is short form for second element '[position() = 2]'
/company/office/@location
selects the 'location' attribute
/AAA/BBB[last()]
last child
/*
all element children of the document root
//*
all element descendants of the document root
//BBB[@name]
elements which have attribute name
//BBB[not(@*)]
elements without an attribute
//BBB[@*]
elements which have any attribute
//BBB[normalize-space(@name)='bbb']
elements with attribute name = bbb, leading and trailing spaces are removed
//*[count(BBB)=2]
elements which have two children BBB
//*[starts-with(name(),'B')]
Select all elements name of which starts with letter B
//*[contains(name(),'C')]
all elements name of which contain letter C
//a | //b
select A or B
//*[string-length(name()) < 3]
elements name of which has one or two characters
//tr[@class='even' or @class='odd']
OR
``
employee/first_name[ . = 'John']
'.' is used to denote the context node 'first_name
.
selects the context node. A dot i.e. '.' is an abbreviated form of the 'self::' axis
../..
the parent element of the parent element of the context node, 'parent::' axis
age/text()
text node of the 'age' child element
``
/child::AAA/child::BBB
equivalent of /AAA/BBB
/descendant::*
all descendants of document root and therefore all elements
/parent::*
all parents of DDD element
//FFF/ancestor::*
all ancestors of all FFF elements (except sublings elements)
/AAA/BBB/following-sibling::*
following-sibling axis contains all the following siblings
//CCC/preceding-sibling::*
contains all the preceding siblings
/following::*
all that after the context node in document order, excluding any descendants and excluding attribute
/ancestor-or-self::*
contains context node and the ancestors of context node, root node
/li[position() mod 2 = 1]
odd elements
//a[contains(@prop,'Foo')]
for xml:
<bla>
<a prop="Foo1"/>
<a prop="Foo2"/>
<a prop="3Foo"/>
<a prop="Bar"/>
</bla>
Consider this:
<html>
<a>Ask Question<other/>
</a>
</html>
//a[.="Ask Question"]
does not return anything! but //a[text()="Ask Question"]
returns all text and <a>
More:
predicate, //div[//x] selects a div if there is an x anywhere in the document, while //div[.//x] selects a div if there is an x within the subtree
//a/text()
will not find text nested in subtree:
<a><other>Ask Question</other> </a>
(//first//*)[1] vs //first//*[1]
//E[count(*)=0]
element with no children
//E[count(../E) = 1]
element with no siblings
Good tester
good xpath guide
good xpath guide
good xpath guide
exercises
XML
cheatsheet
Мануал по XPath