From 3d6a8786bd360b2d4ed5b31c4dfde622bc7b7fd2 Mon Sep 17 00:00:00 2001 From: jaywcjlove Date: Fri, 30 Sep 2022 01:43:43 +0000 Subject: [PATCH] feat: add `xpath.md` cheatsheet. d39380a07f04561eed119b0347398e6d1ef26767 --- docs/xpath.html | 565 ++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + style/style.css | 4 + 3 files changed, 570 insertions(+) create mode 100644 docs/xpath.html diff --git a/docs/xpath.html b/docs/xpath.html new file mode 100644 index 00000000..7bed93f4 --- /dev/null +++ b/docs/xpath.html @@ -0,0 +1,565 @@ + + + + +XPath 备忘清单 + & xpath cheatsheet & Quick Reference + + + + + +

XPath 备忘清单

+

这是一份 XPath 选择器备忘单,其中列出了常用的 XPath 定位方法和 CSS 选择器

+

XPath 选择器

+

入门

+

XPath 即为 XML 路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的计算机语言。

+ +

在 Firefox 或 Chrome 控制台中测试:

+
$x('/html/body')
+$x('//h1')
+$x('//h1')[0].innerText
+$x('//a[text()="XPath"]')[0].click()
+
+

后代选择器

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//h1h1
//div//pdiv p
//ul/liul > li
//ul/li/aul > li > a
//div/*div > *
/:root
/html/body:root > body
+

有序选择器

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//ul/li[1]ul > li:first-child
//ul/li[2]ul > li:nth-child(2)
//ul/li[last()]ul > li:last-child
//li[@id="id"][1]li#id:first-child
//a[1]a:first-child
//a[last()]a:last-child
+

属性选择器

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//*[@id="id"]#id
//*[@class="class"].class
//input[@type="submit"]input[type="submit"]
//a[@id="abc"][@for="xyz"]a#abc[for="xyz"]
//a[@rel]a[rel]
//a[starts-with(@href, '/')]a[href^='/']
//a[ends-with(@href, '.pdf')]a[href$='pdf']
//a[contains(@href, '://')]a[href*='://']
//a[contains(@rel, 'help')]a[rel~='help']
+

兄弟姐妹选择器

+ + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//h1/following-sibling::ulh1 ~ ul
//h1/following-sibling::ul[1]h1 + ul
//h1/following-sibling::[@id="id"]h1 ~ #id
+

jQuery

+ + + + + + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//ul/li/..$('ul > li').parent()
//li/ancestor-or-self::section$('li').closest('section')
//a/@href$('a').attr('href')
//span/text()$('span').text()
+

杂项选择器

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XpathCSS
//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`
+

XPath 表达式

+

步骤和轴

+ + + + + + + + + + + + + + + + + + + + + + + +
----
//ul/a[@id='link']
AxisStepAxisStep
+

前缀

+ + + + + + + + + + + + + + + + + + + + + + + + + +
前缀例子意思是
////hr[@class='edge']任何地方
//html/body/div
././div/p相对的
+

Axes

+ + + + + + + + + + + + + + + + + + + + +
Axis(轴)例子意思是
///ul/li/a孩子
////[@id="list"]//a后裔
+

XPath Predicates(谓词)

+

Predicates(谓词)

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

仅当某些条件为真时才限制节点集。它们可以被链接起来。

+

操作符

+
# 比较
+//a[@id = "xyz"]
+//a[@id != "xyz"]
+//a[@price > 25]
+
+
# 逻辑 (and/or)
+//div[@id="head" and position()=2]
+//div[(x and y) or not(z)]
+
+

使用节点

+
# 在函数内部使用它们
+//ul[count(li) > 2]
+//ul[count(li[@class='hide']) > 0]
+
+
# 返回具有 `<li>` 子级的 `<ul>`
+//ul[li]
+
+

您可以在谓词中使用节点。

+

索引

+
//a[1]                # 第一个<a>
+//a[last()]           # 最后一个<a>
+//ol/li[2]            # 第二个<li>
+//ol/li[position()=2] # 和上面一样
+//ol/li[position()>1] #:not(:first-child)
+
+

[] 与数字一起使用,或者使用 last()position()

+

链接顺序

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

顺序很重要,这两个是不同的。

+

嵌套谓词

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

如果它有一个具有 id='hi'<h1> 后代,则返回 <section>

+

XPath 函数

+

节点功能

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

字符串函数

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

布尔函数

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

类型转换

+
string()
+number()
+boolean()
+
+

XPath Axes

+

使用轴

+
//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
AxisStepAxisStep
+

表达式的步骤由 / 分隔,通常用于选择子节点。 这并不总是正确的:您可以使用 :: 指定不同的“轴”。

+

子轴

+
# 都一样
+//ul/li/a
+//child::ul/child::li/child::a
+
+

child:: is the default axis. This makes //a/b/c work.

+
# 都一样
+# 这是因为 `child::li` 是真实的
+//ul[li]
+//ul[child::li]
+
+
# 都一样
+//ul[count(li) > 2]
+//ul[count(child::li) > 2]
+
+

后代或自我轴

+
# 都一样
+//div//h4
+//div/descendant-or-self::h4
+
+

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

+
# 都一样
+//ul//[last()]
+//ul/descendant-or-self::[last()]
+
+

其他轴

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AxisAbbrevNotes
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
+
+

使用 | 连接两个表达式。

+

XPath 更多示例

+

示例

+
//*                 # 所有元素
+count(//*)          # 计算所有元素
+(//h1)[1]/text()    # 第一个 h1 标题的文本
+//li[span]          # 找到一个 <li> 里面有一个 <span>
+                    # ...扩展到 //li[child::span]
+//ul/li/..          # 使用 .. 选择父级
+
+

查找(父)家长

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

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

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

+ 查找包含 h1#section-name<section>。 + (与上面相同,但使用后代或自我而不是孩子) +

+

最近的

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

jQuery$().closest('.box') 一样工作。

+

属性

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

查找 <item> 并检查其属性

+

另见

+ + +
+ diff --git a/index.html b/index.html index f27d8f95..5c77b20e 100644 --- a/index.html +++ b/index.html @@ -29,6 +29,7 @@ Sketch Jest VSCode + XPath

Linux 命令

diff --git a/style/style.css b/style/style.css index d7d41ba1..fb718173 100644 --- a/style/style.css +++ b/style/style.css @@ -80,6 +80,10 @@ table thead { display: none; border-bottom: solid 1px rgba(85,102,119,0.3); } +table td:first-child>code { + --text-opacity: 1; + color: rgb(5 150 105/var(--text-opacity)); +} table.show-header thead { display: table-header-group;