xml 如何在 IE11 中运行 xPath 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19300859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:12:45  来源:igfitidea点击:

How do you run an xPath query in IE11?

javascriptxmlxpathinternet-explorer-11

提问by pixelmatt

At one point in our system we use javascript to read in a chunk of XML and then query that XML document using xPath.

在我们系统的某个时刻,我们使用 javascript 读取一大块 XML,然后使用 xPath 查询该 XML 文档。

Prior to IE 11, IE supported using xmldoc.selectSingleNode(“//xpath/string”) and the non IE browsers supported using a xmldoc.evaluate(“//xpath/string”). These both returned a similar object that we could then carry on interpreting to extract the data required.

在 IE 11 之前,IE 支持使用 xmldoc.selectSingleNode(“//xpath/string”) 而非 IE 浏览器支持使用 xmldoc.evaluate(“//xpath/string”)。它们都返回了一个类似的对象,然后我们可以继续解释以提取所需的数据。

In IE11 neither of these methods seem to be available.

在 IE11 中,这些方法似乎都不可用。

It seems that IE11 has some support for XML documents in that when I read in the xml using the DOMParser object using the parseFromString method, it returns an object that the IE11 debugger calls an XMLDocument.

似乎 IE11 对 XML 文档有一些支持,因为当我使用 parseFromString 方法使用 DOMParser 对象读取 xml 时,它返回一个 IE11 调试器调用 XMLDocument 的对象。

采纳答案by pixelmatt

Thanks to @Martin Honnen for pointing out that some ActivXObjects are still supported in IE11!

感谢@Martin Honnen 指出 IE11 仍然支持某些 ActivXObjects!

var doc;
try { 
    doc = new ActiveXObject('Microsoft.XMLDOM'); 
    doc.loadXML(stringVarWithXml); 
    var node = doc.selectSingleNode('//foo'); 
} catch (e) { // deal with case that ActiveXObject is not supported }

I've used "Microsoft.XMLDOM" as it is sugested herethat it is a more generic call to what ever xml parser is present on the system, where as it sounds like "Msxml2.DOMDocument.6.0" will fail if that exact version is not present. (We do have to support all IE vers back to 6.0 at my place!)

我使用了“Microsoft.XMLDOM”,因为它在这里被认为是对系统上存在的任何 xml 解析器的更通用的调用,如果该确切版本听起来像“Msxml2.DOMDocument.6.0”将失败不存在。(我们确实必须在我的地方支持所有 IE 版本回到 6.0!)

This just works as it always has done. The only problem I had was that the old switch I used to detect IE vs other browsers was if (typeof ActiveXObject !== "undefined")failed as I guess they are trying to discourage it's use!

这就像往常一样有效。我遇到的唯一问题是我用来检测 IE 与其他浏览器的旧开关if (typeof ActiveXObject !== "undefined")失败了,因为我猜他们正试图阻止它的使用!

Thanks all for your help.

感谢你的帮助。

回答by pingo

To expand on pixelmatt's answer, some results of my tests (Win 7 64bit with IE11) I did in order to get DOMParser to work as it did in IE9 and IE10 (in IE11 it now returns an XMLDocument object which appears to not support xpath queries?).

为了扩展 pixelmatt 的答案,我的一些测试结果(Win 7 64bit with IE11)是为了让 DOMParser 像在 IE9 和 IE10 中一样工作(在 IE11 中它现在返回一个 XMLDocument 对象,该对象似乎不支持 xpath 查询?)。

Turns out I could make it behave like in IE10 with the following meta tag:

事实证明,我可以使用以下元标记使其表现得像在 IE10 中一样:

<meta http-equiv="X-UA-Compatible" content="IE=10" />

Results without and with above meta: IE11 default modeIE11 in IE10 mode

没有和有上述元的结果: IE11 默认模式IE10 模式下的 IE11

And here are the XMLDocument's memebers (for reference): enter image description here

这是 XMLDocument 的成员(供参考): 在此处输入图片说明