Testing

Xpath test bed

Test queries in the Xpath test bed:

Browser console

$x("//div")

Works in Firefox and Chromium browsers.

Selectors

Descendant selectors

CSSXpath?
h1//h1?
div p//div//p?
ul > li//ul/li?
ul > li > a//ul/li/a 
div > *//div/* 
:root/?
:root > body/body 

Attribute selectors

CSSXpath?
#id//*[@id="id"]?
.class//*[@class="class"] ...kinda 
input[type="submit"]//input[@type="submit"] 
a#abc[for="xyz"]//a[@id="abc"][@for="xyz"]?
a[rel]//a[@rel] 
a[href^='/']//a[starts-with(@href, '/')]?
a[href$='pdf']//a[ends-with(@href, '.pdf')] 
a[href*='://']//a[contains(@href, '://')] 
a[rel~='help']//a[contains(@rel, 'help')] ...kinda 

Order selectors

CSSXpath?
ul > li:first-of-type//ul/li[1]?
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"]?
a:first-child//*[1][name()="a"] 
a:last-child//*[last()][name()="a"] 

Siblings

CSSXpath?
h1 ~ ul//h1/following-sibling::ul?
h1 + ul//h1/following-sibling::ul[1] 
h1 ~ #id//h1/following-sibling::[@id="id"] 

jQuery

CSSXpath?
$('ul > li').parent()//ul/li/..?
$('li').closest('section')//li/ancestor-or-self::section 
$('a').attr('href')//a/@href?
$('span').text()//span/text() 

Other things

CSSXpath?
h1:not([id])//h1[not(@id)]?
Text match//button[text()="Submit"]?
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]?
Union (joins results)//a | //div?

Class check

//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).

Expressions

Steps and axes

AxisStepAxisStep
//ul/a[@id='link']
AxisStepAxisStep

Prefixes

PrefixExampleWhat
////hr[@class='edge']Anywhere
././aRelative
//html/body/divRoot

Begin your expression with any of these.

Axes

AxisExampleWhat
///ul/li/aChild
////[@id="list"]//aDescendant

Separate your steps with /. Use two (//) if you don't want to select direct children.

Steps

//div
//div[@name='box']
//[@id='link']

A step may have an element name (div) and predicates ([...]). Both are optional. They can also be these other things:

//a/text()     #=> "Go home"
//a/@href      #=> "index.html"
//a/*          #=> All a's child elements

Predicates

Predicates

//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]

Restricts a nodeset only if some condition is true. They can be chained.

Operators

# Comparison
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
# 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

# Use them inside functions
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
# This returns `
    ` that has a `
  • ` child //ul[li]

You can use nodes inside predicates.

Indexing

Chaining order

Nesting predicates

Functions

Axes

More examples

References

0 Comments for this cheatsheet. Write yours!