DOM Level 3 XPath in Internet Explorer

吃可爱长大的小学妹 提交于 2019-12-28 10:51:50

问题


I just installed IE10 on my Windows 7, and I've noticed that, even if I'm glad that XMLs from AJAX requests are now compatible DOM documents, a rather basic function like document.evaluate is still not supported.

What's worse is that, since those XMLs are not the custom Microsoft IXMLDocument objects, the nodes don't support selectNodes and selectSingleNode anymore. In the end, it seems that IE10 does not support DOM Level 3 XPath or MSXML Xpath.

Seriously, Microsoft? Is there something I'm missing?

Sure, I can use querySelector and querySelectorAll, but I don't want to lose quite a bit of backward compatibility.

Otherwise, one can still request a MSXML document using this line

try {xhr.responseType = "msxml-document";} catch(e) {}

as stated here, but I think it would be nice to deal with, at last, a standard DOM document in IE too.

So, is there a way to use XPath in IE10 with standard DOM documents?

UPDATE 26/7/2013 IE11 isn't stable yet, but it still doesn't support document.evaluate. If it won't support it in the stable release, I doubt it will ever support it. Needless to say this is ridicolous.

I get that you can use querySelector/All in DOM nodes, but it's not supported in IE9 and lower, which are still quite commonly used, and anyway XPath is more powerful than selectors.

Fortunately (if you can say that), you can still set xhr.responseType = "msxml-document". For a moment I feared you couldn't do that anymore...

UPDATE 23/11/2013 IE11 is stable now but, sadly, it doesn't support document.evaluate. As heavy-weight XML documents are used less and less in web applications in favor of JSON or other light notation formats, this is becoming less of a problem, but still.

Setting the responseType property still provides legacy XML documents, so nothing is lost I guess. I don't know if that will stay true for IE12, though.

UPDATE 15/8/2015 Sounds ridiculous right now, but Edge now supports document.evaluate. Just when (almost) everything switched to JSON for data exchange. Well, better late than never, I guess.


回答1:


Yes, there's still no XPath support in IE =/.

For me, the most reliable way to use document.evaluate in every browser is, sadly, via a library.

It's called Wicked Good XPath and it's a recent Google-authored rewrite of the old good JavaScript-XPath library. I've been using Wicked Good XPath since the release and have been really comfortable with it (well, not as much as with a native XPath support, but still).




回答2:


It's part of Microsoft Edge build 10240+: modern.ie:DOM Level 3 XPath

The IE Dev Channel has a build you can use to preview it:

  • Internet Explorer Developer Channel for Windows 7 SP1

  • Internet Explorer Developer Channel for Windows 8.1

It's using wicked-good-xpath internally:

In order to support WGX without polluting a Web page’s context, we created a separate, isolated script engine dedicated to WGX. With a few modifications to WGX that provide entry points for invoking functions and accessing results, we marshal the data from the page to the isolated engine and evaluate expressions with WGX. With WGX enabled to handle native XPath queries, we see immediate gains from sites missing content in our new engine rendering the modern Web

For now, MSDN has a migration guide which recommends the following:

In general, try to migrate to native objects and APIs unless you need features like XPath/XSLT; this can be done by passing responseText to DOMParser, instead of using responseXML var:

If MSXML APIs are still required, feature checks can be updated to verify the type of node received in order to select the correct API:

References

  • Improving interoperability with DOM L3 XPath
  • Native XML object types and legacy websites
  • XMLHttpRequest responseXML in IE10 Release Preview
  • DOMParser and XMLSerializer in IE9 Beta
  • DOMParser and XMLSerializer Demo
  • How To Include Client-Side Script Functions in an XSL Document
  • Details for Document API - Microsoft Edge Development


来源:https://stackoverflow.com/questions/13521554/dom-level-3-xpath-in-internet-explorer

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