--- title: XPath date: 2020-12-19 22:15:43 icon: icon-xpath background: bg-blue-600 tags: - HTML categories: - Toolkit intro: | [XPath](https://en.wikipedia.org/wiki/XPath) or (XML Path Language) is a language that can help you navigate through an XML document using "path like" syntax. --- Testing {.cols-2} -------- ### Xpath test bed Test queries in the Xpath test bed: - [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_ ### Browser console ```js $x("//div") ``` Works in Firefox and Chrome. Selectors {.cols-2} -------- ### Descendant selectors | 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` | | {: .xp} ### Attribute selectors | CSS | Xpath | ? | | ---- | ---- | -- | | `#id` | `//*[@id="id"]` | [?](#predicates) | | `.class` | `//*[@class="class"]` *...[kinda](#class-check)* | | | `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')]` | | | `a[href*='://']` | `//a[contains(@href, '://')]` | | | `a[rel~='help']` | `//a[contains(@rel, 'help')]` *...[kinda](#class-check)* | | {: .xp} ### Order selectors | CSS | Xpath | ? | | ---- | ---- | -- | | `ul > li:first-child` | `//ul/li[1]` | [?](#indexing) | | `ul > li:nth-child(2)` | `//ul/li[2]` | | | `ul > li:last-child` | `//ul/li[last()]` | | | `li#id:first-child` | `//li[@id="id"][1]` | | | `a:first-child` | `//a[1]` | | | `a:last-child` | `//a[last()]` | | {: .xp} ### Siblings | CSS | Xpath | ? | | ---- | ---- | -- | | `h1 ~ ul` | `//h1/following-sibling::ul` | [?](#using-axes) | | `h1 + ul` | `//h1/following-sibling::ul[1]` | | | `h1 ~ #id` | `//h1/following-sibling::[@id="id"]` | | {: .xp} ### jQuery | 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()` | | {: .xp} ### Other things | 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) | {: .xp} ### Class check {.col-span-2} ```bash //div[contains(concat(' ',normalize-space(@class),' '),' foobar ')] ``` 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/)). Expressions {.cols-3} ----------- ### Steps and axes | - | - | - | - | |------|------|------|-----------------| | `//` | `ul` | `/` | `a[@id='link']` | | Axis | Step | Axis | Step | {.left-text} ### Prefixes | Prefix | Example | What | | --- | --- | --- | | `//` | `//hr[@class='edge']` | Anywhere | | `./` | `./a` | Relative | | `/` | `/html/body/div` | Root | {.headers} Begin your expression with any of these. ### Axes | 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. ### Steps {.col-span-2} ```bash //div //div[@name='box'] //[@id='link'] ``` A step may have an element name (`div`) and [predicates](#predicate) (`[...]`). Both are optional. They can also be these other things: ```bash //a/text() #=> "Go home" //a/@href #=> "index.html" //a/* #=> All a's child elements ``` Predicates {.cols-3} ---------- ### Predicates ```bash //div[true()] //div[@class="head"] //div[@class="head"][@id="top"] ``` Restricts a nodeset only if some condition is true. They can be chained. ### Operators ```bash # Comparison //a[@id = "xyz"] //a[@id != "xyz"] //a[@price > 25] ``` ```bash # Logic (and/or) //div[@id="head" and position()=2] //div[(x and y) or not(z)] ``` Use comparison and logic operators to make conditionals. ### Using nodes ```bash # Use them inside functions //ul[count(li) > 2] //ul[count(li[@class='hide']) > 0] ``` ```bash # Returns `