using xpaths in javascript

2019-08-23 04:26发布

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

2条回答
ゆ 、 Hurt°
2楼-- · 2019-08-23 05:01

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.

查看更多
够拽才男人
3楼-- · 2019-08-23 05:06

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:

This looks interesting too:

查看更多
登录 后发表回答