Code & computing / QUICK REFERENCE

XPath selectors: navigation and predicates

Find HTML or XML nodes by element, position, class, attributes and text using browser-compatible XPath 1.0.

49 entries

Select nodes and paths

All anchors
//a

Finds a elements anywhere beneath the document root. [1][3]

All list items
//li

Selects all li elements regardless of depth. [1][3]

Children of nav
//nav/a

Matches anchor elements that are immediate nav children. [1][3]

Deep links inside nav
//nav//a

Matches anchor descendants at any depth below nav. [1][3]

Root element
/html

Absolute path from the HTML document root. [1][3]

Current node
.

Refers to the context node supplied to the evaluator. [1][3]

Current node descendants
.//span

Search below a selected context node, not the entire document. [1][3]

Parent node
..

Selects a node's parent in XPath's abbreviated syntax. [1][3]

Direct parent axis
parent::div

A div parent, if that node is the parent. [1][3]

Ancestor section
ancestor::section

Selects every section ancestor of the current node. [1][3]

Descendant text elements
//article/descendant::p

All p descendants of an article, nested or direct. [1][3]

Union of node sets
//h1 | //h2

Returns h1 and h2 nodes as a combined node set. [1][3]

Wildcard children
//ul/*

All element children of every ul. [1][3]

One level after article
//article/*

Matches direct article element children only. [1][3]

Filter by attributes and text

Element with an ID
//*[@id='main']

Match an element whose id attribute is exactly main. [1][4]

Button with type
//button[@type='submit']

Restricts button elements to submit type. [1][4]

Attribute existence
//*[@data-test]

Selects elements with a data-test attribute, regardless of its value. [1][4]

Exact class attribute
//*[@class='card']

Only class attributes equal to card exactly; excludes multiple-class strings. [1][4]

CSS class token
//*[contains(concat(' ',normalize-space(@class),' '),' card ')]

Token-aware membership test; avoids matching postcard. [1][4]

Attribute prefix
//a[starts-with(@href,'/docs/')]

Links whose href attribute begins with /docs/. [1][4]

Attribute contains
//a[contains(@href,'help')]

Finds links whose URL attribute contains a literal substring. [1][4]

Has child span
//button[span]

Selects buttons having an immediate span child. [1][4]

Has descendant icon
//button[.//svg]

Selects buttons containing a descendant SVG element when namespace matching permits. [1][4]

Exact direct text
//p[text()='Done']

A direct text child must equal Done; nested markup is not concatenated here. [1][4]

Normalize text spacing
//p[normalize-space(.)='Done']

Collapses whitespace in the element's string-value before comparison. [1][4]

Text fragment
//p[contains(.,'Error')]

Tests whether the element's combined descendant string-value contains Error. [1][4]

Not an attribute match
//input[not(@disabled)]

Excludes disabled HTML input elements by attribute presence. [1][4]

Multiple conditions
//input[@type='text' and @name='email']

All named predicate conditions must be true. [1][4]

One of two conditions
//input[@type='email' or @type='tel']

Selects input elements of either listed type. [1][4]

Positions and relatives

First matching list item
(//li)[1]

First li in the full selected document order. [3][4]

First li in each ul
//ul/li[1]

First direct li child for each matching ul. [3][4]

Last item in each list
//ul/li[last()]

Each ul's final direct li child. [3][4]

Second item in each list
//ul/li[position()=2]

Second direct list item under each ul. [3][4]

Last matching global item
(//li)[last()]

Parentheses group the entire li node set before applying last(). [3][4]

Following sibling
//h2/following-sibling::p[1]

First later p sibling after each heading, if one exists. [3][4]

Preceding sibling
//p/preceding-sibling::h2[1]

Nearest earlier h2 sibling on the reverse axis. [3][4]

Nearest enclosing form
//input/ancestor::form[1]

Closest form ancestor of each input. [3][4]

Self axis
//button[self::button]

Explicitly retain nodes that are button elements. [3][4]

Elements with sibling
//li[following-sibling::li]

Every li that has at least one later li sibling. [3][4]

Elements with no children
//div[not(*)]

Divs with no element children; text nodes may still exist. [3][4]

Count children
//ul[count(li)=3]

Selects ul elements with exactly three direct li children. [3][4]

XPath in JavaScript

First node
document.evaluate('//h1',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue

Returns the first matching element or null. [2][1]

Snapshot result
document.evaluate('//a',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null)

Retrieve snapshotLength and snapshotItem(i) to read matching nodes. [2][1]

String result
document.evaluate('string(//title)',document,null,XPathResult.STRING_TYPE,null).stringValue

Reads the title element's string-value. [2][1]

Numeric count
document.evaluate('count(//li)',document,null,XPathResult.NUMBER_TYPE,null).numberValue

Number of selected list items. [2][1]

Boolean result
document.evaluate('boolean(//form)',document,null,XPathResult.BOOLEAN_TYPE,null).booleanValue

Whether at least one form exists. [2][1]

Context-scoped lookup
document.evaluate('.//span',someElement,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null)

Search within a supplied element when it is in scope. [2][1]

Iterate results
result.iterateNext()

With an iterator result type, yield the next node until null. [2][1]

Namespace caution
document.evaluate('//svg',document,null,XPathResult.ANY_TYPE,null)

In XML or namespaced SVG contexts, name tests and namespace resolvers require special care. [2][1]

Reserved placement, separate from reference controls. No ad network is active.

Updated XPath 1.0 · browser DOM

Sources

  1. XPath basics ↗
  2. XPathEvaluator.evaluate() ↗
  3. XPath axes ↗
  4. XPath functions ↗