Advanced XPaths
General Form
XPaths generally take the following form:
/axis1::E1[C1]/axis2::E2[C2]
axis
are navigation axes.C
are conditions.E
are name or a tag, attribute or*
.
Navigation Axes
Axis | Shorthand | Description |
---|---|---|
attribute |
@ |
An attribute |
child |
Default (child:: can be omitted) |
Any child |
descendant |
//E instead of /descendant::E |
Any proper descendant |
descendant-or-self |
Any descendant | |
ancestor |
Any proper ancestor | |
following-sibling |
Any sibling to the right | |
preceding-sibling |
Any sibling to the left | |
parent |
.. |
The parent |
self |
. |
Yourself |
A proper descendant or ancestor is at least one node away (not self).
Conditions
The idea is the if the condition is true, follow the path further.
- You can use comparitors like
=
,!=
and>=
.- A value can be a relative path expression or any constant.
- You can combine by using
and
andor
.
//book[category="CS"]/title
You can also write E[i]
which is true if the current node is the i
-th child of its parent:
//*[category="CS" or category="Scifi"][1]
This would return the first item in “CS” or “Scifi.
You can also use functions like:
last()
in place of i
.
Boolean Conversions
We can also include additional XPaths and strings in the condition:
-
Returns the titles of all book that have a category:
//book[category]/title
-
Returns the books if the price attribute is non-empty:
//book[@price/data()]