Xpath 选择器速查表

选择器

后代选择器

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

属性选择器

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

顺序选择器

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

兄弟节点

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

其他

CSSXpath?
h1:not([id])//h1[not(@id)]?
文本匹配//button[text()="Submit"]?
文本匹配(包含)//button[contains(text(),"Go")]
数值比较//product[@price > 2.50]
含子元素//ul[*]
含特定子元素//ul[li]
或逻辑//a[@name or @href]?
并集(合并结果)//a | //div?

Class check

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

Xpath 没有“判断是否包含空格分隔列表项”的内置运算符,这是常用的替代写法。

表达式

步骤与轴

//ul/a[@id='link']
步骤步骤

前缀

前缀示例含义
////hr[@class='edge']任意位置
././a相对路径
//html/body/div根节点

表达式可以从任意前缀开始。

示例含义
///ul/li/a子节点
////[@id="list"]//a后代节点

/ 分隔步骤;如果不想只选直接子节点,用 //

步骤

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

一个步骤可以包含元素名(如 div)和谓词[...]),二者都可省略。 还可以是以下形式:

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

谓词

谓词

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

只有条件为真时才保留节点集。谓词可以链式叠加。

运算符

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

节点可以直接用于谓词判断。

索引

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

[] 中使用数字、last()position() 进行索引。

链式顺序

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

顺序不同,结果也不同。

嵌套谓词

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

<section> 含有 id='hi' 的 <h1> 后代时匹配。

函数

节点函数

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]

布尔函数

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

字符串函数

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

类型转换

string()
number()
boolean()

使用轴

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

表达式的步骤通常用 / 分隔来选择子节点,但也可以用 :: 指定不同的轴。

//ul/child::li
步骤步骤

子轴

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

child:: 是默认轴,因此 //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]

后代或自身轴

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

//descendant-or-self:: 轴的简写。

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

其他轴

缩写说明
ancestor
ancestor-or-self
attribute@@hrefattribute::href 的简写
childdivchild::div 的简写
descendant
descendant-or-self/////descendant-or-self::node()/ 的简写
namespace
self..self::node() 的简写
parent....parent::node() 的简写
following
following-sibling
preceding
preceding-sibling

还有更多轴可用。

并集

//a | //span

使用 | 合并两个表达式结果。

更多示例

示例

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

查找父级

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

查找直接包含 h1#section-name<section>

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

查找包含 h1#section-name<section>。 (与上面类似,但使用 descendant-or-self 而不是 child)

最近祖先

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

类似于 jQuery 的 $().closest('.box')

属性

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

查找 <item> 并检查其属性。

测试

浏览器控制台

$x("//div")