问题
I have a large set of XPaths for selecting content in webpages and I want users to be able to use them in the browser (including IE).
What do you recommend?
Try and interpret the XPaths with JavaScript?
Or perhaps convert to regex?
Some existing JavaScript XPath work:
http://js-xpath.sourceforge.net/xpath-example.html
http://goog-ajaxslt.sourceforge.net
回答1:
I would look for an XSLT javascript library. Since most modern browsers have built-in XSLT support, and XSLT includes support for XPath, it is possible to use that engine to power your XPath selectors.
Personally, I've used Sarissa and the Glyphix jQuery.xslTransform libraries successfully:
- http://jquery.glyphix.com/
- http://dev.abiss.gr/sarissa/
This looks interesting too:
- http://johannburkard.de/software/xsltjs/
回答2:
Nowadays browsers support the XPath 1.0 based DOM 3 XPath out of the box. The main API is the document.evaluate
function which is available in all mayor desktop browsers except IE.
And there are polyfills, if you want to use it in older browser versions or IE.
来源:https://stackoverflow.com/questions/3695146/using-xpaths-in-javascript