diff --git a/README.md b/README.md index 7916acd..2d2818f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Quick Reference [Sketch](./docs/sketch.md) [Jest](./docs/jest.md) [VSCode](./docs/vscode.md) +[XPath](./docs/xpath.md) ## Linux 命令 diff --git a/docs/xpath.md b/docs/xpath.md new file mode 100644 index 0000000..fb9b156 --- /dev/null +++ b/docs/xpath.md @@ -0,0 +1,389 @@ +XPath 备忘清单 +=== + +这是一份 XPath 选择器备忘单,其中列出了常用的 `XPath` 定位方法和 `CSS` 选择器 + +XPath 选择器 +-------- + +### 入门 + +XPath 即为 XML 路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的计算机语言。 + +- [Xpath test bed](http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm) _(whitebeam.org)_ + +在 Firefox 或 Chrome 控制台中测试: + +```js +$x('/html/body') +$x('//h1') +$x('//h1')[0].innerText +$x('//a[text()="XPath"]')[0].click() +``` + +### 后代选择器 + +| Xpath | CSS | +|--------------|--------------| +| `//h1` | h1 | +| `//div//p` | div p | +| `//ul/li` | ul > li | +| `//ul/li/a` | ul > li > a | +| `//div/*` | div > * | +| `/` | :root | +| `/html/body` | :root > body | + + +### 有序选择器 + +| Xpath | CSS | +|---------------------|----------------------| +| `//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 | + + +### 属性选择器 + + +| Xpath | CSS | +|---------------------------------|----------------------| +| `//*[@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'] | + + +### 兄弟姐妹选择器 + +| Xpath | CSS | +|--------------------------------------|----------| +| `//h1/following-sibling::ul` | h1 ~ ul | +| `//h1/following-sibling::ul[1]` | h1 + ul | +| `//h1/following-sibling::[@id="id"]` | h1 ~ #id | + + +### jQuery + +| Xpath | CSS | +|----------------------------------|----------------------------| +| `//ul/li/..` | $('ul > li').parent() | +| `//li/ancestor-or-self::section` | $('li').closest('section') | +| `//a/@href` | $('a').attr('href') | +| `//span/text()` | $('span').text() | + + +### 杂项选择器 + +| Xpath | CSS | +|-----------------------------------|---------------------------| +| `//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']` | +| Axis | Step | Axis | Step | + + +### 前缀 + +| 前缀 | 例子 | 意思是 | +|--------|-----------------------|----------| +| `//` | `//hr[@class='edge']` | 任何地方 | +| `/` | `/html/body/div` | 根 | +| `./` | `./div/p` | 相对的 | + + +### Axes + +| Axis(轴) | 例子 | 意思是 | +|------|---------------------|------------| +| `/` | `//ul/li/a` | 孩子 | +| `//` | `//[@id="list"]//a` | 后裔 | + + +XPath Predicates(谓词) +---------- + +### Predicates(谓词) + +```bash +//div[true()] +//div[@class="head"] +//div[@class="head"][@id="top"] +``` + +仅当某些条件为真时才限制节点集。它们可以被链接起来。 + +### 操作符 + +```bash +# 比较 +//a[@id = "xyz"] +//a[@id != "xyz"] +//a[@price > 25] +``` + +```bash +# 逻辑 (and/or) +//div[@id="head" and position()=2] +//div[(x and y) or not(z)] +``` + +### 使用节点 + +```bash +# 在函数内部使用它们 +//ul[count(li) > 2] +//ul[count(li[@class='hide']) > 0] +``` +```bash +# 返回具有 `
  • ` 子级的 `