8 $x('//div//p//*') == $('div p *'), $x('//[@id="item"]') == $('#item'), and many other Xpath examples.
15 Test queries in the Xpath test bed:<br>
16 [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_
24 Works in Firefox and Chrome.
28 ### Descendant selectors
32 | `h1` | `//h1` | [?](#prefixes) |
33 | `div p` | `//div//p` | [?](#axes) |
34 | `ul > li` | `//ul/li` | [?](#axes) |
35 | `ul > li > a` | `//ul/li/a` | |
36 | `div > *` | `//div/*` | |
38 | `:root` | `/` | [?](#prefixes) |
39 | `:root > body` | `/body` | |
42 ### Attribute selectors
46 | `#id` | `//*[@id="id"]` | [?](#predicates) |
47 | `.class` | `//*[@class="class"]` *...[kinda](#class-check)* | |
48 | `input[type="submit"]` | `//input[@type="submit"]` | |
49 | `a#abc[for="xyz"]` | `//a[@id="abc"][@for="xyz"]` | [?](#chaining-order) |
50 | `a[rel]` | `//a[@rel]` | |
52 | `a[href^='/']` | `//a[starts-with(@href, '/')]` | [?](#string-functions) |
53 | `a[href$='pdf']` | `//a[ends-with(@href, '.pdf')]` | |
54 | `a[href*='://']` | `//a[contains(@href, '://')]` | |
55 | `a[rel~='help']` | `//a[contains(@rel, 'help')]` *...[kinda](#class-check)* | |
62 | `ul > li:first-child` | `//ul/li[1]` | [?](#indexing) |
63 | `ul > li:nth-child(2)` | `//ul/li[2]` | |
64 | `ul > li:last-child` | `//ul/li[last()]` | |
65 | `li#id:first-child` | `//li[@id="id"][1]` | |
66 | `a:first-child` | `//a[1]` | |
67 | `a:last-child` | `//a[last()]` | |
74 | `h1 ~ ul` | `//h1/following-sibling::ul` | [?](#using-axes) |
75 | `h1 + ul` | `//h1/following-sibling::ul[1]` | |
76 | `h1 ~ #id` | `//h1/following-sibling::[@id="id"]` | |
83 | `$('ul > li').parent()` | `//ul/li/..` | [?](#other-axes) |
84 | `$('li').closest('section')` | `//li/ancestor-or-self::section` | |
85 | `$('a').attr('href')` | `//a/@href` | [?](#steps) |
86 | `$('span').text()` | `//span/text()` | |
93 | `h1:not([id])` | `//h1[not(@id)]` | [?](#boolean-functions) |
94 | Text match | `//button[text()="Submit"]` | [?](#operators) |
95 | Text match (substring) | `//button[contains(text(),"Go")]` | |
96 | Arithmetic | `//product[@price > 2.50]` | |
97 | Has children | `//ul[*]` | |
98 | Has children (specific) | `//ul[li]` | |
99 | Or logic | `//a[@name or @href]` | [?](#operators) |
100 | Union (joins results) | `//a | //div` | [?](#unions) |
104 /* ensure tables align */
105 table.xp {table-layout: fixed;}
106 table.xp tr>:nth-child(1) {width: 35%;}
107 table.xp tr>:nth-child(2) {width: auto;}
108 table.xp tr>:nth-child(3) {width: 10%; text-align:right;}
114 //div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]
117 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/)).
124 | `//` | `ul` | `/` | `a[@id='link']` |
125 | Axis | Step | Axis | Step |
130 | Prefix | Example | What |
132 | `//` | `//hr[@class='edge']` | Anywhere |
133 | `./` | `./a` | Relative |
134 | `/` | `/html/body/div` | Root |
137 Begin your expression with any of these.
141 | Axis | Example | What |
143 | `/` | `//ul/li/a` | Child |
144 | `//` | `//[@id="list"]//a` | Descendant |
147 Separate your steps with `/`. Use two (`//`) if you don't want to select direct children.
157 A step may have an element name (`div`) and [predicates](#predicate) (`[...]`). Both are optional.
158 They can also be these other things:
161 //a/text() #=> "Go home"
162 //a/@href #=> "index.html"
163 //a/* #=> All a's child elements
174 //div[@class="head"][@id="top"]
177 Restricts a nodeset only if some condition is true. They can be chained.
190 //div[@id="head" and position()=2]
191 //div[(x and y) or not(z)]
194 Use comparison and logic operators to make conditionals.
199 # Use them inside functions
201 //ul[count(li[@class='hide']) > 0]
205 # This returns `<ul>` that has a `<li>` child
209 You can use nodes inside predicates.
215 //a[last()] # last <a>
216 //ol/li[2] # second <li>
217 //ol/li[position()=2] # same as above
218 //ol/li[position()>1] # :not(:first-child)
221 Use `[]` with a number, or `last()` or `position()`.
230 Order is significant, these two are different.
232 ### Nesting predicates
235 //section[//h1[@id='hi']]
238 This returns `<section>` if it has an `<h1>` descendant with `id='hi'`.
246 name() # //[starts-with(name(), 'h')]
247 text() # //button[text()="Submit"]
254 count() # //table[count(tr)=1]
255 position() # //ol/li[position()=2]
258 ### Boolean functions
261 not(expr) # button[not(starts-with(text(),"Submit"))]
267 contains() # font[contains(@class,"head")]
268 starts-with() # font[starts-with(@class,"head")]
269 ends-with() # font[ends-with(@class,"head")]
274 substring(str, start, len)
275 substring-before("01/02", "/") #=> 01
276 substring-after("01/02", "/") #=> 02
297 //ul/child::li # ul > li (same)
298 //ul/following-sibling::li # ul ~ li
299 //ul/descendant-or-self::li # ul li
300 //ul/ancestor-or-self::li # $('ul').closest('li')
303 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 `::`.
305 | `//` | `ul` | `/child::` | `li` |
306 | Axis | Step | Axis | Step |
314 //child::ul/child::li/child::a
317 `child::` is the default axis. This makes `//a/b/c` work.
321 # this works because `child::li` is truthy, so the predicate succeeds
329 //ul[count(child::li) > 2]
332 ### Descendant-or-self axis
337 //div/descendant-or-self::h4
340 `//` is short for the `descendant-or-self::` axis.
345 //ul/descendant-or-self::[last()]
350 | Axis | Abbrev | Notes |
353 | `ancestor-or-self` | | |
355 | `attribute` | `@` | `@href` is short for `attribute::href` |
356 | `child` | | `div` is short for `child::div` |
358 | `descendant-or-self` | `//` | `//` is short for `/descendant-or-self::node()/` |
361 | `self` | `.` | `.` is short for `self::node()` |
362 | `parent` | `..` | `..` is short for `parent::node()` |
365 | `following-sibling` | | |
367 | `preceding-sibling` | | |
370 There are other axes you can use.
378 Use `|` to join two expressions.
387 count(//*) # count all elements
388 (//h1)[1]/text() # text of the first h1 heading
389 //li[span] # find a <li> with an <span> inside it
390 # ...expands to //li[child::span]
391 //ul/li/.. # use .. to select a parent
397 //section[h1[@id='section-name']]
399 Finds a `<section>` that directly contains `h1#section-name`
402 //section[//h1[@id='section-name']]
405 Finds a `<section>` that contains `h1#section-name`.
406 (Same as above, but uses descendant-or-self instead of child)
411 ./ancestor-or-self::[@class="box"]
414 Works like jQuery's `$().closest('.box')`.
419 //item[@price > 2*@discount]
422 Finds `<item>` and check its attributes
428 * [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_