昨天,今天,明天,每天的每天,你是否都多懂得一点点...

星期二, 三月 09, 2010

XPATH 中 descendant 和 // 的区别


//td/descendant::font
//td//font


上面两句得到的结果是相同的, 在这种情况下,两者没有区别.

但是


//td/descendant::font[1]
//td//font[1]


这样,两者就有区别了. 举个例子


一得到的结果

td
font         <- 
font
font
font




二得到的结果

td
font         <- 
font <-
font
font
font <-





//td/descendant::font[2]
//td//font[2]

一得到的结果

td
font         
font <-
font
font




二得到的结果

td
font         
font
font <-
font <-
font


descendant 加入序号以后只会给出一个结果,也就是所有的TD 下,只会抓一个FONT. 这里的 2 的作用是, 抓第二级的 font, 也就是 font 下面的 font. 

而 // 下, 这里的 2 意思就不同了, 这里的 2 是指, 在同级别下的第二个 font. 


由于 descendant 的序号是抓级别的, 有时候会很有用, 比如说我想抓某个 table 中的 第一级的那个 table, 而不是所有的 table, 我们可以这样

tag/descendant::table[1]


如果用 tag//table[1] 那抓出来的会是所以子级别中的第一个 table, 可就不是我们想要的了. 


--
Feng

1 条评论:

匿名 说...

on the latest online [url=http://freecasinogames2010.webs.com]casino games[/url]. vs the all fashion [url=http://www.realcazinoz.com/]casino games[/url] guide. looking on account of [url=http://www.avi.vg/]sex[/url] ? or latest [url=http://bestcasinos.webs.com/]casino games[/url] ? [url=http://www.realcazinoz.com/amex-casinos.htm]Amex Casino[/url] . infrequent more [url=http://gravatar.com/amexcasinos]amex casino[/url] sites.
[url=http://freecasinogames2010.webs.com/onlineblackjack.htm]online blackjack[/url] .

其它博客地址

此博客的同步博客地址: http://fengnz.wordpress.com
这里进入我的MSN SPACE.