views:

202

answers:

1

how can i convert xpath like

/html/body/div[3]/ul/li[1]/a[5]

html > body > div[3] > ul > li[1] > a[5]

i believe, index is not supported with CSS3 selectors....so how to deal with this ?

+3  A: 

If you find that Sizzle/jQuery can't apply your CSS3 selector it might be better to use the XPath plugin which was part of the original release of jQuery (and then removed since few people actually used it).

XPath implementations in browsers tends to be much faster than the CSS engines. Also having JS parse & convert an XPath expression into CSS3 then having jQuery munge that into something the browser can implement (generally CSS2.1 selectors with a bit of JS assistance) is going to be much slower than executing the XPath directly in the browser.

Not only that, but there are things that XPath can do that CSS can't. For example:
//h3[class="blog-title"]/../../div[class="blog-entry"]//input[fn:floor(value) > 3]
which isn't overly complex for XPath to execute, but impossible for CSS alone - moving back up the DOM and executing a function as part of the expression can't (to my knowledge) be done yet, even in CSS3.

digitala