Xpath Cheatsheet: In-Text Search
Xpath Cheatsheet: In-Text Search
Xpath Cheatsheet: In-Text Search
Expression Description
nodename Select all nodes with the name “nodename”
/ A beginning single slash indicates a select from the root node, subsequent slashes indicate selecting a child node from current node
// Select direct and indirect child nodes in the document from the current node - this gives you the ability to “skip levels”
. Select the current context node
.. Select the parent of the context node
@ Select attributes
text() Select the value of an element
| Pipe chains expressions and brings back results from either expression, think of a set union
Operator Explanation
= Equivalent comparison, can be used for numeric or text values
!= Is not equivalent comparison
>, >= Greater than, greater than or equal to
<, <= Less than, less than or equal to
or Boolean or
and Boolean and
not Boolean not
Predicates
| [1] |Select the first node|
| [last()] |Select the last node|
| [last()-1] |Select the last but one node (also known as the second last node)|
|[position()<3]|Select the first two nodes, note the first position starts at 1, not = |
|[@lang]| Select nodes that have attribute ‘lang’|
|[@lang='en']| Select all the nodes that have a “attribute” attribute with a value of “en”|
|[price>15.00]| Select all nodes that have a price node with a value greater than 15.00|
Wildcards
Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
In-text search
XPath can do in-text searching using functions and also supports regex with its matches() function. Note: in-text searching is case-sensitive!
Examples
HTML View
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Layout</title>
</head>
<body>
<div class="wrapper">
<header>
<h1>Yoko's Kitchen</h1>
<nav>
<ul>
<li>
<a href="home" class="current">home</a>
</li>
<li>
<a href="classes">classes</a>
</li>
<li>
<a href="catering">catering</a>
</li>
<li>
<a href="about">about</a>
</li>
<li>
<a href="contact">contact</a>
</li>
</ul>
</nav>
</header>
<section class="courses">
<a href="introduction.html">
<article>
<figure>
<img src="images/bok-choi.jpg" alt="Bok Choi" />
<figcaption>Bok Choi</figcaption>
</figure>
<hgroup>
<h2>Japanese Vegetarian</h2>
<h3>Five week course in London</h3>
</hgroup>
<p>A five week introduction to traditional Japanese vegetarian meals, teaching you a selection of rice and noodle dishes.</p>
</article>
</a>
<a href="sauces.html">
<article>
<figure>
<img src="images/teriyaki.jpg" alt="Teriyaki sauce" />
<figcaption>Teriyaki Sauce</figcaption>
</figure>
<hgroup>
<h2>Sauces Masterclass</h2>
<h3>One day workshop</h3>
</hgroup>
<p>An intensive one-day course looking at how to create the most delicious sauces for use in a range of Japanese cookery.</p>
</article>
</a>
</section>
<aside>
<section class="popular-recipes">
<h2>Popular Recipes</h2>
<a href="yakitori.png">Yakitori (grilled chicken)</a>
<a href="tsukune.png">Tsukune (minced chicken patties)</a>
<a href="okonomiyaki.png">Okonomiyaki (savory pancakes)</a>
<a href="mizutaki.png">Mizutaki (chicken stew)</a>
</section>
<section class="contact-details">
<h2>Contact</h2>
<p>Yoko's Kitchen
<br />
27 Redchurch Street
<br />
Shoreditch
<br />
London E2 7DP
</p>
</section>
</aside>
<footer>
© 2011 Yoko's Kitchen
</footer>
</div>
<!-- .wrapper -->
</body>
</html>