Guía rápida de XPath

Selectores

Selectores descendientes

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

Selectores por atributo

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

Selectores de orden

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"]

Hermanos

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()

Otros

CSSXpath?
h1:not([id])//h1[not(@id)]?
Coincidencia de texto//button[text()="Submit"]?
Coincidencia (subcadena)//button[contains(text(),"Go")]
Comparación numérica//product[@price > 2.50]
Tiene hijos//ul[*]
Tiene hijos (específico)//ul[li]
Lógica OR//a[@name or @href]?
Unión (combina resultados)//a | //div?

Class check

//div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]

Xpath no tiene un operador para comprobar si un valor está en una lista separada por espacios; esta es la forma habitual.

Expresiones

Pasos y ejes

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

Prefijos

PrefijoEjemploSignificado
////hr[@class='edge']En cualquier lugar
././aRelativo
//html/body/divRaíz

Puedes comenzar una expresión con cualquiera de estos.

Ejes

EjeEjemploSignificado
///ul/li/aHijo
////[@id="list"]//aDescendiente

Separa pasos con /. Usa // si no quieres seleccionar solo hijos directos.

Pasos

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

Un paso puede tener un nombre de elemento (div) y predicados ([...]). Ambos son opcionales. También puede ser algo como:

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

Predicados

Predicados

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

Restringe un conjunto de nodos solo si la condición es verdadera. Se pueden encadenar.

Operadores

# 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)]

Usa operadores de comparación y lógica para construir condiciones.

Usar nodos

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

Puedes usar nodos directamente dentro de predicados.

Indexación

//a[1]                  # first <a>
//a[last()]             # last <a>
//ol/li[2]              # second <li>
//ol/li[position()=2]   # same as above
//ol/li[position()>1]   # :not(:first-of-type)

Usa [] con un número, o last() o position().

Orden de encadenado

a[1][@href='/']
a[@href='/'][1]

El orden importa; estos dos son distintos.

Predicados anidados

//section[.//h1[@id='hi']]

Devuelve <section> si tiene un <h1> descendiente con id='hi'.

Funciones

Funciones de nodo

name()                     # //[starts-with(name(), 'h')]
text()                     # //button[text()="Submit"]
                           # //button/text()
lang(str)
namespace-uri()
count()                    # //table[count(tr)=1]
position()                 # //ol/li[position()=2]

Funciones booleanas

not(expr)                  # button[not(starts-with(text(),"Submit"))]

Funciones de cadena

contains()                 # font[contains(@class,"head")]
starts-with()              # font[starts-with(@class,"head")]
ends-with()                # font[ends-with(@class,"head")]
concat(x,y)
substring(str, start, len)
substring-before("01/02", "/")  #=> 01
substring-after("01/02", "/")   #=> 02
translate()
normalize-space()
string-length()

Conversión de tipos

string()
number()
boolean()

Ejes

Uso de ejes

//ul/li                       # ul > li
//ul/child::li                # ul > li (same)
//ul/following-sibling::li    # ul ~ li
//ul/descendant-or-self::li   # ul li
//ul/ancestor-or-self::li     # $('ul').closest('li')

Los pasos suelen separarse con / para seleccionar hijos, pero puedes indicar otro eje con ::.

//ul/child::li
EjePasoEjePaso

Eje child

# both the same
//ul/li/a
//child::ul/child::li/child::a

child:: es el eje por defecto. Por eso //a/b/c funciona.

# both the same
# this works because `child::li` is truthy, so the predicate succeeds
//ul[li]
//ul[child::li]
# both the same
//ul[count(li) > 2]
//ul[count(child::li) > 2]

Eje descendant-or-self

# both the same
//div//h4
//div/descendant-or-self::h4

// es abreviatura del eje descendant-or-self::.

# both the same
//ul//[last()]
//ul/descendant-or-self::[last()]

Otros ejes

EjeAbrevNotas
ancestor
ancestor-or-self
attribute@@href es abreviatura de attribute::href
childdiv es abreviatura de child::div
descendant
descendant-or-self//// es abreviatura de /descendant-or-self::node()/
namespace
self.. es abreviatura de self::node()
parent.... es abreviatura de parent::node()
following
following-sibling
preceding
preceding-sibling

Hay más ejes disponibles.

Uniones

//a | //span

Usa | para unir dos expresiones.

Más ejemplos

Ejemplos

//*                 # all elements
count(//*)          # count all elements
(//h1)[1]/text()    # text of the first h1 heading
//li[span]          # find a <li> with an <span> inside it
                    # ...expands to //li[child::span]
//ul/li/..          # use .. to select a parent

Buscar un padre

//section[h1[@id='section-name']]

Encuentra un <section> que contiene directamente h1#section-name.

//section[//h1[@id='section-name']]

Encuentra un <section> que contiene h1#section-name. (Igual que arriba, pero usando descendant-or-self en lugar de child)

Closest

./ancestor-or-self::[@class="box"]

Funciona como $().closest('.box') de jQuery.

Atributos

//item[@price > 2*@discount]

Encuentra <item> y comprueba sus atributos.

Pruebas

Consola del navegador

$x("//div")