è¿æ¯ä¸ä»½ XPath éæ©å¨å¤å¿åï¼å
¶ä¸ååºäºå¸¸ç¨ç XPath å®ä½æ¹æ³å CSS éæ©å¨
XPath å³ä¸º XML è·¯å¾è¯è¨ï¼XML Path Languageï¼ï¼å®æ¯ä¸ç§ç¨æ¥ç¡®å®XMLææ¡£ä¸æé¨åä½ç½®çè®¡ç®æºè¯è¨ã
ç±»ä¼¼è·¯å¾ è¯æ³è¯å«åå¯¼èª XML ææ¡£ä¸çèç¹å¨ Firefox æ Chrome æ§å¶å°ä¸æµè¯ï¼
$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 |
| Xpath | CSS |
|---|---|
nodename | éæ©å称为 nodename çææèç¹ |
/ | 仿 ¹èç¹ä¸éæ© |
// | ä»å½åèç¹ä¸éæ©ææ¡£ä¸ä¸éæ©å¹é çèç¹ï¼æ 论å®ä»¬å¨åªé |
. | éæ©å½åèç¹ |
.. | éæ©å½åèç¹çç¶èç¹ |
@ | 鿩屿§ |
| 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 | CSS |
|---|---|
//ul/li/.. | $('ul > li').parent() |
//li/ancestor-or-self::section | $('li').closest('section') |
//a/@href | $('a').attr('href') |
//span/text() | $('span').text() |
| è¿ç®ç¬¦ | 说æ | ç¤ºä¾ |
|---|---|---|
| | 计ç®ä¸¤ä¸ªèç¹é | //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 |
and | å | price>9.00 and price<9.90 |
mod | 模æ°ï¼é¤æ³ä½æ°ï¼ | 5 mod 2 |
| - | - | - | - |
|---|---|---|---|
// | ul | / | a[@id='link'] |
| Axis | Step | Axis | Step |
| åç¼ | ä¾å | æææ¯ |
|---|---|---|
// | //hr[@class='edge'] | ä»»ä½å°æ¹ |
/ | /html/body/div | æ ¹ |
./ | ./div/p | ç¸å¯¹ç |
| Axis(è½´) | ä¾å | æææ¯ |
|---|---|---|
/ | //ul/li/a | å©å |
// | //[@id="list"]//a | åè£ |
//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>ã
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()
//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 |
| Axis | Step | Axis | Step |
表达å¼çæ¥éª¤ç± / åéï¼é常ç¨äºéæ©åèç¹ã è¿å¹¶ä¸æ»æ¯æ£ç¡®çï¼æ¨å¯ä»¥ä½¿ç¨ :: æå®ä¸åçâè½´âã
# é½ä¸æ ·
//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()]
| Axis | Abbrev | Notes |
|---|---|---|
ancestor | éæ©å½åèç¹çææç¥å (ç¶æ¯ãç¥ç¶æ¯ç) | |
ancestor-or-self | éæ©å½åèç¹ææç¥å (ç¶ãç¥ç¶ç)åå½åèç¹æ¬èº« | |
attribute | @ | @href æ¯ attribute::href ç缩å |
child | div æ¯ child::div ç缩å | |
descendant | éæ©å½åèç¹çææå代ï¼åãåçï¼ | |
descendant-or-self | // | // æ¯ /descendant-or-self::node()/ç缩å éæ©å½åèç¹åå½åèç¹æ¬èº«çææå代ï¼åãåçï¼ |
namespace | éæ©å½åèç¹çææå½å空é´èç¹ | |
self | . | . æ¯ self::node() ç缩åï¼éæ©å½åèç¹ |
parent | .. | .. æ¯ parent::node() ç缩åï¼éæ©å½åèç¹çç¶èç¹ |
following | éæ©ææ¡£ä¸å½åèç¹ç»ææ è®°ä¹åçææå 容 | |
following-sibling | éæ©å½åèç¹ä¹åçææå å¼èç¹ | |
preceding | éæ©ææ¡£ä¸åºç°å¨å½åèç¹ä¹åçææèç¹ï¼é¤äºç¥å ã屿§èç¹åå½å空é´èç¹ | |
preceding-sibling | éæ©å½åèç¹ä¹åçææå å¼èç¹ |
æ¨è¿å¯ä»¥ä½¿ç¨å ¶ä»è½´ã
//a | //span
ä½¿ç¨ | è¿æ¥ä¸¤ä¸ªè¡¨è¾¾å¼ã
//* # ææå
ç´
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> å¹¶æ£æ¥å
¶å±æ§