XPath‑Spickzettel

Selektoren

Nachfahren‑Selektoren

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

Attribut‑Selektoren

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

Reihenfolge‑Selektoren

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

Geschwister

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

Sonstiges

CSSXpath?
h1:not([id])//h1[not(@id)]?
Textabgleich//button[text()="Submit"]?
Textabgleich (Teilstring)//button[contains(text(),"Go")]
Arithmetik//product[@price > 2.50]
Hat Kinder//ul[*]
Hat bestimmte Kinder//ul[li]
ODER‑Logik//a[@name or @href]?
Vereinigung (Ergebnisse)//a | //div?

Class check

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

Xpath hat keinen Operator „Teil einer leerzeichen‑getrennten Liste“. Das ist der übliche Workaround.

Ausdrücke

Schritte und Achsen

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

Präfixe

PräfixBeispielBedeutung
////hr[@class='edge']Überall
././aRelativ
//html/body/divRoot

Beginne den Ausdruck mit einem dieser Präfixe.

Achsen

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

Trenne Schritte mit /. Nutze //, wenn es nicht nur direkte Kinder sein sollen.

Schritte

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

Ein Schritt kann einen Elementnamen (div) und Prädikate ([...]) haben. Beides ist optional. Auch möglich:

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

Prädikate

Prädikate

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

Begrenzt den Node‑Satz nur, wenn die Bedingung wahr ist. Prädikate können verkettet werden.

Operatoren

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

Nutze Vergleichs‑ und Logikoperatoren für Bedingungen.

Nodes verwenden

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

Nodes können direkt in Prädikaten genutzt werden.

Indexierung

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

Verwende [] mit Zahl, last() oder position().

Verkettungsreihenfolge

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

Die Reihenfolge ist wichtig; diese beiden unterscheiden sich.

Verschachtelte Prädikate

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

Gibt <section> zurück, wenn es einen <h1>‑Nachfahren mit id='hi' hat.

Funktionen

Node‑Funktionen

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]

Boolesche Funktionen

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

String‑Funktionen

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

Typumwandlung

string()
number()
boolean()

Achsen

Achsen verwenden

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

Schritte werden mit / getrennt, können aber mit :: einen anderen Axis nutzen.

//ul/child::li
AchseSchrittAchseSchritt

Child‑Achse

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

child:: ist die Standardachse. Deshalb funktioniert //a/b/c.

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

Descendant‑or‑self‑Achse

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

// ist die Kurzform der descendant-or-self::‑Achse.

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

Andere Achsen

AchseKürzelHinweise
ancestor
ancestor-or-self
attribute@@href ist Kurzform von attribute::href
childdiv ist Kurzform von child::div
descendant
descendant-or-self//// ist Kurzform von /descendant-or-self::node()/
namespace
self.. ist Kurzform von self::node()
parent.... ist Kurzform von parent::node()
following
following-sibling
preceding
preceding-sibling

Es gibt weitere Achsen.

Vereinigungen

//a | //span

| verbindet zwei Ausdrücke.

Weitere Beispiele

Beispiele

//*                 # 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

Parent finden

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

Findet ein <section>, das h1#section-name direkt enthält.

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

Findet ein <section>, das h1#section-name enthält. (Gleiches Ergebnis, aber mit descendant-or-self statt child)

Nächstes Eltern‑Element

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

Entspricht jQuerys $().closest('.box').

Attribute

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

Findet <item> und prüft Attribute.

Testen

Browser‑Konsole

$x("//div")