国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

XPath 備忘清單

這是一份 XPath 選擇器備忘單,其中列出了常用的 XPath 定位方法和 CSS 選擇器

XPath 選擇器

入門

XPath 即為 XML 路徑語言(XML Path Language),它是一種用來確定XML文檔中某部分位置的計算機語言。

  • Xpath test bed (whitebeam.org)
  • XPath 代表 XML 路徑語言
  • XPath 使用 類似路徑 語法識別和導航 XML 文檔中的節(jié)點
  • XPath 包含超過 200 個內置函數(shù)
  • XPath 是 XSLT 標準中的主要元素
  • XPath 是 W3C 推薦的

FirefoxChrome 控制臺中測試:

$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

最有用的路徑表達式

XpathCSS
nodename選擇名稱為 nodename 的所有節(jié)點
/從根節(jié)點中選擇
//從當前節(jié)點中選擇文檔中與選擇匹配的節(jié)點,無論它們在哪里
.選擇當前節(jié)點
..選擇當前節(jié)點的父節(jié)點
@選擇屬性

雜項選擇器

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聯(lián)合(加入結果)

jQuery

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

運算符

運算符說明示例
|計算兩個節(jié)點集//book | //cd
+添加6 + 4
-減法6 - 4
*乘法6 * 4
div分配8 div 4
=平等的price=9.80
!=不相等price!=9.80
<小于price<9.80
<=小于或等于price<=9.80
>大于price>9.80
>=大于或等于price>=9.80
or或者price=9.80 or price=9.70
andprice>9.00 and price<9.90
mod模數(shù)(除法余數(shù))5 mod 2

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

僅當某些條件為真時才限制節(jié)點集。它們可以被鏈接起來。

操作符

# 比較
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
# 邏輯 (and/or)
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]

使用節(jié)點

# 在函數(shù)內部使用它們
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
# 返回具有 `<li>` 子級的 `<ul>`
//ul[li]

您可以在謂詞中使用節(jié)點。

索引

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

[] 與數(shù)字一起使用,或者使用 last()position()

鏈接順序

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

順序很重要,這兩個是不同的。

嵌套謂詞

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

如果它有一個具有 id='hi'<h1> 后代,則返回 <section>。

XPath 函數(shù)

節(jié)點功能

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]

字符串函數(shù)

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

布爾函數(shù)

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

表達式的步驟由 / 分隔,通常用于選擇子節(jié)點。 這并不總是正確的:您可以使用 :: 指定不同的“軸”。

子軸

# 都一樣
//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選擇當前節(jié)點的所有祖先(父母、祖父母等)
ancestor-or-self選擇當前節(jié)點所有祖先(父、祖父等)和當前節(jié)點本身
attribute@@hrefattribute::href 的縮寫
childdivchild::div 的縮寫
descendant選擇當前節(jié)點的所有后代(子、孫等)
descendant-or-self/////descendant-or-self::node()/的縮寫 選擇當前節(jié)點和當前節(jié)點本身的所有后代(子、孫等)
namespace選擇當前節(jié)點的所有命名空間節(jié)點
self..self::node() 的縮寫,選擇當前節(jié)點
parent....parent::node() 的縮寫,選擇當前節(jié)點的父節(jié)點
following選擇文檔中當前節(jié)點結束標記之后的所有內容
following-sibling選擇當前節(jié)點之后的所有兄弟節(jié)點
preceding選擇文檔中出現(xiàn)在當前節(jié)點之前的所有節(jié)點,除了祖先、屬性節(jié)點和命名空間節(jié)點
preceding-sibling選擇當前節(jié)點之前的所有兄弟節(jié)點

您還可以使用其他軸。

聯(lián)合

//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> 并檢查其屬性

另見