32

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.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • 8
    Wow! Just Googled around a bit and you may be onto something. This would make reason #9678 no one should be using IE! – Rob Nov 23 '12 at 00:08
  • I thought `querySelector/All` **was** supported in IE9. –  Jun 06 '15 at 03:52
  • @torazaburo `querySelector/All` supports the CSS selector language. [XPath](https://developer.mozilla.org/en-US/docs/Web/XPath) is a (more powerful) language to select/analyze nodes in *ML documents. The point of this post is to question the existence of a cross-browser selector language for XML documents. – MaxArt Jun 06 '15 at 08:09
  • I know what `querySelector/All` is and what XPath is. I was merely referring to the statement in your post saying "but it's not supported in IE9 and lower". This is incorrect. It should be "IE8 and lower". I'm not saying that makes a meaningful difference to this question or your answer. –  Jun 06 '15 at 08:50
  • @torazaburo `querySelector` is supported by IE8 too, but not on XML nodes. IE10 is the first IE that handles XMLs as standard DOM documents. – MaxArt Jun 06 '15 at 12:42
  • 1
    Thanks for the periodic status updates to this question! – anton.burger Apr 15 '16 at 14:43
  • I just stumbled into this space for the first time, doing some scraping in a context where I happened to have an IHTMLDocument at hand (ordinarily I'd go with [HTML Agility Pack](https://stackoverflow.com/q/846994/876993)). It's intriguing and peculiar that XSL, which includes a dialect of XPath, has been in browsers for ages but that the XPath implementation needed for XSL was not exposed in IE (is that correct?). I'm sure I'm missing some [interesting historical details](https://stackoverflow.com/q/2099880/876993) here. . . – unbob Sep 19 '17 at 18:35

2 Answers2

15

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).

JDB
  • 25,172
  • 5
  • 72
  • 123
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • 1
    *still*? In the question, it sounds as if there had been XPath support before and it has been discontinued. – O. R. Mapper Nov 23 '12 at 10:50
  • @O.R.Mapper As far as I know, no version of IE has ever supported `document.evaluate` (or any other XPath selecting on DOM documents). – Petr Janeček Nov 23 '12 at 11:03
  • 3
    @Slanec Indeed, but the XMLs you got from AJAX request up to IE9 where MSXML documents, and the nodes had these handy `.selectNodes` and `.selectSingleNode` you could use in the (partial) place of `document.evaluate`. Since IE10, the default response document isn't an MSXML document anymore, so those methods are gone. Your answer sounds good anyway, I'll accept it if no better answers show up in a couple of days. – MaxArt Nov 23 '12 at 14:22
  • Try this website: http://llamalab.com/js/xpath/ They have a 15k version minified and a demo page to test their script out. It works great. – PHPGuru Dec 12 '13 at 05:28
5

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:

It's using 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

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265