./
也可以不写斜杠及之前的内容。
以此开头(或开头直接是节点)的以相对位置看待。
/
以此开头的以绝对位置看待。
../
选取直接在 a
下的 b
节点:
a/b
选取 a
下的 b
节点,不管是直接在 a
下的,还是间接的:
a//b
从根节点下选取 a
节点,不管是直接在根节点下的,还是间接的:
//a
选取 a
元素之后的兄弟 b
元素。a
与 b
同级,非亲子关系:
a/following-sibling::b
选取 a
元素之前的兄弟 b
元素。
a/preceding-sibling::b
preceding-sibling
中,若结果有多个,在指定下标的时候,从离锚元素最近的元素开始,从 1
算起。如:
a/preceding-sibling::b[2]
指 a
元素向前数,第二个兄弟 b
元素。
XPath 中以 1
为开始。
选取 parent
下的第 n
个 child
元素:
parent/child[n]
选取 parent
下的最后一个 child
元素:
parent/child[last()]
选取 parent
下的倒数第二个 child
元素:
parent/child[last()-1]
选取 parent
下的前两个 child
元素:
parent/child[position()<3]
若涉及到锚元素,则下标从距离锚元素最近的目标元素开始数起,到最远处,不一定是从上到下。
选取 a
节点下的 b
节点,和 c
节点下的 d
节点:
a/b | c/d