8 $x('//div//p//*') == $('div p *'), $x('//[@id="item"]') == $('#item'), and many other Xpath examples.
16 Test queries in the Xpath test bed:
18 - [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_
26 Works in Firefox and Chrome.
30 ### Descendant selectors
34 | `h1` | `//h1` | [?](#prefixes) |
35 | `div p` | `//div//p` | [?](#axes) |
36 | `ul > li` | `//ul/li` | [?](#axes) |
37 | `ul > li > a` | `//ul/li/a` | |
38 | `div > *` | `//div/*` | |
40 | `:root` | `/` | [?](#prefixes) |
41 | `:root > body` | `/body` | |
44 ### Attribute selectors
48 | `#id` | `//*[@id="id"]` | [?](#predicates) |
49 | `.class` | `//*[@class="class"]` *...[kinda](#class-check)* | |
50 | `input[type="submit"]` | `//input[@type="submit"]` | |
51 | `a#abc[for="xyz"]` | `//a[@id="abc"][@for="xyz"]` | [?](#chaining-order) |
52 | `a[rel]` | `//a[@rel]` | |
54 | `a[href^='/']` | `//a[starts-with(@href, '/')]` | [?](#string-functions) |
55 | `a[href$='pdf']` | `//a[ends-with(@href, '.pdf')]` | |
56 | `a[href*='://']` | `//a[contains(@href, '://')]` | |
57 | `a[rel~='help']` | `//a[contains(@rel, 'help')]` *...[kinda](#class-check)* | |
64 | `ul > li:first-of-type` | `//ul/li[1]` | [?](#indexing) |
65 | `ul > li:nth-of-type(2)` | `//ul/li[2]` | |
66 | `ul > li:last-of-type` | `//ul/li[last()]` | |
67 | `li#id:first-of-type` | `//li[1][@id="id"]` | [?](#chaining-order) |
68 | `a:first-child` | `//*[1][name()="a"]` | |
69 | `a:last-child` | `//*[last()][name()="a"]` | |
76 | `h1 ~ ul` | `//h1/following-sibling::ul` | [?](#using-axes) |
77 | `h1 + ul` | `//h1/following-sibling::ul[1]` | |
78 | `h1 ~ #id` | `//h1/following-sibling::[@id="id"]` | |
85 | `$('ul > li').parent()` | `//ul/li/..` | [?](#other-axes) |
86 | `$('li').closest('section')` | `//li/ancestor-or-self::section` | |
87 | `$('a').attr('href')` | `//a/@href` | [?](#steps) |
88 | `$('span').text()` | `//span/text()` | |
95 | `h1:not([id])` | `//h1[not(@id)]` | [?](#boolean-functions) |
96 | Text match | `//button[text()="Submit"]` | [?](#operators) |
97 | Text match (substring) | `//button[contains(text(),"Go")]` | |
98 | Arithmetic | `//product[@price > 2.50]` | |
99 | Has children | `//ul[*]` | |
100 | Has children (specific) | `//ul[li]` | |
101 | Or logic | `//a[@name or @href]` | [?](#operators) |
102 | Union (joins results) | `//a | //div` | [?](#unions) |
106 /* ensure tables align */
107 table.xp {table-layout: fixed;}
108 table.xp tr>:nth-child(1) {width: 35%;}
109 table.xp tr>:nth-child(2) {width: auto;}
110 table.xp tr>:nth-child(3) {width: 10%; text-align:right;}
116 //div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]
119 Xpath doesn't have the "check if part of space-separated list" operator, so this is the workaround ([source](http://pivotallabs.com/xpath-css-class-matching/)).
126 | `//` | `ul` | `/` | `a[@id='link']` |
127 | Axis | Step | Axis | Step |
132 | Prefix | Example | What |
134 | `//` | `//hr[@class='edge']` | Anywhere |
135 | `./` | `./a` | Relative |
136 | `/` | `/html/body/div` | Root |
139 Begin your expression with any of these.
143 | Axis | Example | What |
145 | `/` | `//ul/li/a` | Child |
146 | `//` | `//[@id="list"]//a` | Descendant |
149 Separate your steps with `/`. Use two (`//`) if you don't want to select direct children.
159 A step may have an element name (`div`) and [predicates](#predicate) (`[...]`). Both are optional.
160 They can also be these other things:
163 //a/text() #=> "Go home"
164 //a/@href #=> "index.html"
165 //a/* #=> All a's child elements
176 //div[@class="head"][@id="top"]
179 Restricts a nodeset only if some condition is true. They can be chained.
192 //div[@id="head" and position()=2]
193 //div[(x and y) or not(z)]
196 Use comparison and logic operators to make conditionals.
201 # Use them inside functions
203 //ul[count(li[@class='hide']) > 0]
207 # This returns `<ul>` that has a `<li>` child
211 You can use nodes inside predicates.
217 //a[last()] # last <a>
218 //ol/li[2] # second <li>
219 //ol/li[position()=2] # same as above
220 //ol/li[position()>1] # :not(:first-of-type)
223 Use `[]` with a number, or `last()` or `position()`.
232 Order is significant, these two are different.
234 ### Nesting predicates
237 //section[.//h1[@id='hi']]
240 This returns `<section>` if it has an `<h1>` descendant with `id='hi'`.
248 name() # //[starts-with(name(), 'h')]
249 text() # //button[text()="Submit"]
256 count() # //table[count(tr)=1]
257 position() # //ol/li[position()=2]
260 ### Boolean functions
263 not(expr) # button[not(starts-with(text(),"Submit"))]
269 contains() # font[contains(@class,"head")]
270 starts-with() # font[starts-with(@class,"head")]
271 ends-with() # font[ends-with(@class,"head")]
276 substring(str, start, len)
277 substring-before("01/02", "/") #=> 01
278 substring-after("01/02", "/") #=> 02
299 //ul/child::li # ul > li (same)
300 //ul/following-sibling::li # ul ~ li
301 //ul/descendant-or-self::li # ul li
302 //ul/ancestor-or-self::li # $('ul').closest('li')
305 Steps of an expression are separated by `/`, usually used to pick child nodes. That's not always true: you can specify a different "axis" with `::`.
307 | `//` | `ul` | `/child::` | `li` |
308 | Axis | Step | Axis | Step |
316 //child::ul/child::li/child::a
319 `child::` is the default axis. This makes `//a/b/c` work.
323 # this works because `child::li` is truthy, so the predicate succeeds
331 //ul[count(child::li) > 2]
334 ### Descendant-or-self axis
339 //div/descendant-or-self::h4
342 `//` is short for the `descendant-or-self::` axis.
347 //ul/descendant-or-self::[last()]
352 | Axis | Abbrev | Notes |
355 | `ancestor-or-self` | | |
357 | `attribute` | `@` | `@href` is short for `attribute::href` |
358 | `child` | | `div` is short for `child::div` |
360 | `descendant-or-self` | `//` | `//` is short for `/descendant-or-self::node()/` |
363 | `self` | `.` | `.` is short for `self::node()` |
364 | `parent` | `..` | `..` is short for `parent::node()` |
367 | `following-sibling` | | |
369 | `preceding-sibling` | | |
372 There are other axes you can use.
380 Use `|` to join two expressions.
389 count(//*) # count all elements
390 (//h1)[1]/text() # text of the first h1 heading
391 //li[span] # find a <li> with an <span> inside it
392 # ...expands to //li[child::span]
393 //ul/li/.. # use .. to select a parent
399 //section[h1[@id='section-name']]
401 Finds a `<section>` that directly contains `h1#section-name`
404 //section[//h1[@id='section-name']]
407 Finds a `<section>` that contains `h1#section-name`.
408 (Same as above, but uses descendant-or-self instead of child)
413 ./ancestor-or-self::[@class="box"]
416 Works like jQuery's `$().closest('.box')`.
421 //item[@price > 2*@discount]
424 Finds `<item>` and check its attributes
430 * [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_