using xpaths in javascript

放肆的年华 提交于 2020-01-06 19:50:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!