cheatsheets/xpath.md

430 lines
14 KiB
Markdown
Raw Permalink Normal View History

2014-10-07 07:52:58 +00:00
---
title: Xpath
2015-11-24 05:09:17 +00:00
category: HTML
2017-08-28 16:37:00 +00:00
tags: [Featured]
2017-08-29 15:35:17 +00:00
weight: -5
2017-10-14 18:53:08 +00:00
description: |
$x('//div//p//*') == $('div p *'), $x('//[@id="item"]') == $('#item'), and many other Xpath examples.
2014-10-23 11:38:50 +00:00
---
2014-10-07 07:52:58 +00:00
2017-08-30 09:34:48 +00:00
## Testing
### Xpath test bed
2020-07-05 14:38:31 +00:00
{: .-intro}
2017-08-30 09:34:48 +00:00
2020-07-05 14:38:31 +00:00
Test queries in the Xpath test bed:
- [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_
2017-08-30 09:34:48 +00:00
### Browser console
```js
2019-11-06 17:17:37 +00:00
$x("//div")
2017-08-30 09:34:48 +00:00
```
Works in Firefox and Chrome.
2017-08-29 22:50:54 +00:00
## Selectors
2015-04-17 06:54:36 +00:00
### Descendant selectors
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
| `h1` | `//h1` | [?](#prefixes) |
| `div p` | `//div//p` | [?](#axes) |
| `ul > li` | `//ul/li` | [?](#axes) |
| `ul > li > a` | `//ul/li/a` | |
| `div > *` | `//div/*` | |
| ---- | ---- | -- |
| `:root` | `/` | [?](#prefixes) |
| `:root > body` | `/body` | |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 06:54:36 +00:00
### Attribute selectors
2015-04-17 06:12:29 +00:00
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
2018-03-07 15:09:01 +00:00
| `#id` | `//*[@id="id"]` | [?](#predicates) |
| `.class` | `//*[@class="class"]` *...[kinda](#class-check)* | |
2015-04-17 08:28:35 +00:00
| `input[type="submit"]` | `//input[@type="submit"]` | |
| `a#abc[for="xyz"]` | `//a[@id="abc"][@for="xyz"]` | [?](#chaining-order) |
| `a[rel]` | `//a[@rel]` | |
| ---- | ---- | -- |
| `a[href^='/']` | `//a[starts-with(@href, '/')]` | [?](#string-functions) |
| `a[href$='pdf']` | `//a[ends-with(@href, '.pdf')]` | |
2018-02-11 16:38:10 +00:00
| `a[href*='://']` | `//a[contains(@href, '://')]` | |
| `a[rel~='help']` | `//a[contains(@rel, 'help')]` *...[kinda](#class-check)* | |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 07:57:05 +00:00
2015-04-17 11:23:59 +00:00
### Order selectors
2015-04-17 07:57:05 +00:00
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
| `ul > li:first-of-type` | `//ul/li[1]` | [?](#indexing) |
| `ul > li:nth-of-type(2)` | `//ul/li[2]` | |
| `ul > li:last-of-type` | `//ul/li[last()]` | |
| `li#id:first-of-type` | `//li[1][@id="id"]` | [?](#chaining-order) |
| `a:first-child` | `//*[1][name()="a"]` | |
| `a:last-child` | `//*[last()][name()="a"]` | |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 08:06:52 +00:00
2015-04-17 08:28:35 +00:00
### Siblings
2015-04-17 08:06:52 +00:00
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
2015-04-17 08:59:18 +00:00
| `h1 ~ ul` | `//h1/following-sibling::ul` | [?](#using-axes) |
2015-04-17 08:28:35 +00:00
| `h1 + ul` | `//h1/following-sibling::ul[1]` | |
| `h1 ~ #id` | `//h1/following-sibling::[@id="id"]` | |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 08:06:52 +00:00
2015-04-17 06:54:36 +00:00
### jQuery
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
| `$('ul > li').parent()` | `//ul/li/..` | [?](#other-axes) |
| `$('li').closest('section')` | `//li/ancestor-or-self::section` | |
| `$('a').attr('href')` | `//a/@href` | [?](#steps) |
| `$('span').text()` | `//span/text()` | |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 06:54:36 +00:00
### Other things
2015-04-17 08:28:35 +00:00
| CSS | Xpath | ? |
| ---- | ---- | -- |
| `h1:not([id])` | `//h1[not(@id)]` | [?](#boolean-functions) |
| Text match | `//button[text()="Submit"]` | [?](#operators) |
| Text match (substring) | `//button[contains(text(),"Go")]` | |
| Arithmetic | `//product[@price > 2.50]` | |
| Has children | `//ul[*]` | |
| Has children (specific) | `//ul[li]` | |
| Or logic | `//a[@name or @href]` | [?](#operators) |
| Union (joins results) | `//a | //div` | [?](#unions) |
2017-08-29 22:50:54 +00:00
{: .xp}
2015-04-17 08:28:35 +00:00
<style>
/* ensure tables align */
table.xp {table-layout: fixed;}
2017-08-29 22:50:54 +00:00
table.xp tr>:nth-child(1) {width: 35%;}
2015-04-17 08:28:35 +00:00
table.xp tr>:nth-child(2) {width: auto;}
table.xp tr>:nth-child(3) {width: 10%; text-align:right;}
</style>
2015-04-17 06:54:36 +00:00
2015-04-17 06:12:29 +00:00
### Class check
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
//div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]
```
2017-08-29 22:50:54 +00:00
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/)).
2015-04-17 06:12:29 +00:00
Expressions
-----------
2017-08-29 22:50:54 +00:00
### Steps and axes
| `//` | `ul` | `/` | `a[@id='link']` |
| Axis | Step | Axis | Step |
{: .-css-breakdown}
2014-10-07 07:52:58 +00:00
### Prefixes
2017-08-29 22:50:54 +00:00
| Prefix | Example | What |
| --- | --- | --- |
| `//` | `//hr[@class='edge']` | Anywhere |
| `./` | `./a` | Relative |
| `/` | `/html/body/div` | Root |
{: .-headers}
Begin your expression with any of these.
2014-10-07 07:52:58 +00:00
2015-04-17 06:12:29 +00:00
### Axes
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
| Axis | Example | What |
| --- | --- | --- |
| `/` | `//ul/li/a` | Child |
| `//` | `//[@id="list"]//a` | Descendant |
{: .-headers}
Separate your steps with `/`. Use two (`//`) if you don't want to select direct children.
2014-10-07 07:52:58 +00:00
2015-04-17 06:17:43 +00:00
### Steps
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:43 +00:00
//div
//div[@name='box']
//[@id='link']
```
2017-08-29 22:50:54 +00:00
A step may have an element name (`div`) and [predicates](#predicate) (`[...]`). Both are optional.
They can also be these other things:
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
```bash
//a/text() #=> "Go home"
//a/@href #=> "index.html"
//a/* #=> All a's child elements
2015-04-17 06:12:29 +00:00
```
Predicates
----------
2017-08-29 22:50:54 +00:00
### Predicates
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
```bash
2018-02-11 16:38:10 +00:00
//div[true()]
2015-04-17 06:12:29 +00:00
//div[@class="head"]
//div[@class="head"][@id="top"]
```
2017-08-29 22:50:54 +00:00
Restricts a nodeset only if some condition is true. They can be chained.
2015-04-17 06:12:29 +00:00
### Operators
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
# Comparison
2017-08-29 22:50:54 +00:00
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
# Logic (and/or)
2017-08-29 22:50:54 +00:00
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
Use comparison and logic operators to make conditionals.
2015-04-17 06:12:29 +00:00
### Using nodes
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
# Use them inside functions
2017-08-29 22:50:54 +00:00
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
# This returns `<ul>` that has a `<li>` child
2017-08-29 22:50:54 +00:00
//ul[li]
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
You can use nodes inside predicates.
2015-04-17 06:17:22 +00:00
### Indexing
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
//a[1] # first <a>
//a[last()] # last <a>
//ol/li[2] # second <li>
2015-04-17 06:17:43 +00:00
//ol/li[position()=2] # same as above
//ol/li[position()>1] # :not(:first-of-type)
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
Use `[]` with a number, or `last()` or `position()`.
2015-04-17 06:17:43 +00:00
### Chaining order
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:12:29 +00:00
a[1][@href='/']
a[@href='/'][1]
```
2017-08-29 22:50:54 +00:00
Order is significant, these two are different.
2015-04-17 06:17:22 +00:00
### Nesting predicates
2015-04-17 06:12:29 +00:00
```
//section[.//h1[@id='hi']]
2015-04-17 06:17:22 +00:00
```
2017-08-29 22:50:54 +00:00
This returns `<section>` if it has an `<h1>` descendant with `id='hi'`.
2015-04-17 06:12:29 +00:00
Functions
---------
### Node functions
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
name() # //[starts-with(name(), 'h')]
text() # //button[text()="Submit"]
# //button/text()
lang(str)
namespace-uri()
2017-08-29 22:50:54 +00:00
```
2015-04-17 06:17:22 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
count() # //table[count(tr)=1]
position() # //ol/li[position()=2]
```
2014-10-07 07:52:58 +00:00
2015-04-17 06:12:29 +00:00
### Boolean functions
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:43 +00:00
not(expr) # button[not(starts-with(text(),"Submit"))]
2015-04-17 06:17:22 +00:00
```
2015-04-17 06:12:29 +00:00
### String functions
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
contains() # font[contains(@class,"head")]
starts-with() # font[starts-with(@class,"head")]
ends-with() # font[ends-with(@class,"head")]
2017-08-29 22:50:54 +00:00
```
2015-04-17 06:17:22 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
concat(x,y)
substring(str, start, len)
substring-before("01/02", "/") #=> 01
substring-after("01/02", "/") #=> 02
translate()
normalize-space()
string-length()
```
2015-04-17 06:12:29 +00:00
### Type conversion
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:22 +00:00
string()
number()
boolean()
```
2015-04-17 06:12:29 +00:00
Axes
----
2015-04-17 06:54:36 +00:00
### Using axes
2015-04-17 06:17:43 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 08:57:10 +00:00
//ul/li # ul > li
//ul/child::li # ul > li (same)
//ul/following-sibling::li # ul ~ li
//ul/descendant-or-self::li # ul li
2015-04-17 06:17:43 +00:00
//ul/ancestor-or-self::li # $('ul').closest('li')
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
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 `::`.
| `//` | `ul` | `/child::` | `li` |
| Axis | Step | Axis | Step |
{: .-css-breakdown}
2015-04-17 06:12:29 +00:00
### Child axis
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:43 +00:00
# both the same
2017-08-29 22:50:54 +00:00
//ul/li/a
//child::ul/child::li/child::a
2015-04-17 06:17:43 +00:00
```
2017-08-29 22:50:54 +00:00
`child::` is the default axis. This makes `//a/b/c` work.
```bash
2015-04-17 06:17:43 +00:00
# both the same
# this works because `child::li` is truthy, so the predicate succeeds
2017-08-29 22:50:54 +00:00
//ul[li]
//ul[child::li]
2015-04-17 06:17:43 +00:00
```
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:43 +00:00
# both the same
2017-08-29 22:50:54 +00:00
//ul[count(li) > 2]
//ul[count(child::li) > 2]
2015-04-17 06:12:29 +00:00
```
2015-04-17 08:54:29 +00:00
### Descendant-or-self axis
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 06:17:43 +00:00
# both the same
2017-08-29 22:50:54 +00:00
//div//h4
//div/descendant-or-self::h4
2015-04-17 08:54:29 +00:00
```
2015-04-17 06:12:29 +00:00
2017-08-29 22:50:54 +00:00
`//` is short for the `descendant-or-self::` axis.
```bash
2015-04-17 06:17:43 +00:00
# both the same
2017-08-29 22:57:28 +00:00
//ul//[last()]
//ul/descendant-or-self::[last()]
2015-04-17 06:12:29 +00:00
```
### Other axes
2017-08-29 22:50:54 +00:00
| Axis | Abbrev | Notes |
2015-04-17 06:12:29 +00:00
| --- | --- | --- |
| `ancestor` | | |
| `ancestor-or-self` | | |
| --- | --- | --- |
| `attribute` | `@` | `@href` is short for `attribute::href` |
| `child` | | `div` is short for `child::div` |
| `descendant` | | |
| `descendant-or-self` | `//` | `//` is short for `/descendant-or-self::node()/` |
| `namespace` | | |
| --- | --- | --- |
| `self` | `.` | `.` is short for `self::node()` |
| `parent` | `..` | `..` is short for `parent::node()` |
| --- | --- | --- |
| `following` | | |
| `following-sibling` | | |
| `preceding` | | |
| `preceding-sibling` | | |
2017-08-29 22:50:54 +00:00
{: .-headers}
There are other axes you can use.
2015-04-17 06:12:29 +00:00
2015-04-17 08:06:52 +00:00
### Unions
2017-08-29 22:50:54 +00:00
```bash
2015-04-17 08:06:52 +00:00
//a | //span
```
2017-08-29 22:50:54 +00:00
Use `|` to join two expressions.
2015-04-17 06:12:29 +00:00
More examples
-------------
2017-08-29 22:50:54 +00:00
### Examples
```bash
2015-04-17 06:12:29 +00:00
//* # all elements
count(//*) # count all elements
2015-04-17 06:54:36 +00:00
(//h1)[1]/text() # text of the first h1 heading
2015-04-17 06:17:43 +00:00
//li[span] # find a <li> with an <span> inside it
2015-04-17 06:12:29 +00:00
# ...expands to //li[child::span]
//ul/li/.. # use .. to select a parent
```
2017-08-29 22:50:54 +00:00
### Find a parent
```bash
//section[h1[@id='section-name']]
2015-04-17 06:12:29 +00:00
```
2017-08-29 22:50:54 +00:00
Finds a `<section>` that directly contains `h1#section-name`
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
```bash
//section[//h1[@id='section-name']]
2015-04-17 06:12:29 +00:00
```
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
Finds a `<section>` that contains `h1#section-name`.
(Same as above, but uses descendant-or-self instead of child)
### Closest
```bash
./ancestor-or-self::[@class="box"]
2015-04-17 06:17:43 +00:00
```
2017-08-29 22:50:54 +00:00
Works like jQuery's `$().closest('.box')`.
### Attributes
```bash
//item[@price > 2*@discount]
2015-04-17 06:12:29 +00:00
```
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
Finds `<item>` and check its attributes
2015-04-17 06:12:29 +00:00
References
----------
2017-08-29 22:50:54 +00:00
{: .-one-column}
2014-10-07 07:52:58 +00:00
2017-08-29 22:50:54 +00:00
* [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_