Javascript 对象不支持 Internet Explorer 10 (Windows 8) 中的属性或方法“transformNode”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12149410/
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
Object doesn't support property or method 'transformNode' in Internet Explorer 10 (Windows 8)
提问by John Chapman
I am having some JavaScript issues that seem to only occur in Internet Explorer 10 on Windows 8 (IE 7, 8, and 9 all work fine). The basic jist of what I am doing is getting XML and XSL from a web service and then transforming them in JavaScript to render on the page using the Sys.Net.XMLDOM object.
我遇到了一些 JavaScript 问题,这些问题似乎只出现在 Windows 8 上的 Internet Explorer 10 中(IE 7、8 和 9 都可以正常工作)。我所做的基本任务是从 Web 服务获取 XML 和 XSL,然后在 JavaScript 中将它们转换为使用 Sys.Net.XMLDOM 对象呈现在页面上。
XMLDOM = Sys.Net.XMLDOM;
var xsl = // XSL gotten from somewhere else
var xmlString = // XML gotten from somewhere else as a string...
var xml = new XMLDOM(xmlString);
var content = xml.transformNode(xsl);
When I use the above code in IE 10, I get:
当我在 IE 10 中使用上面的代码时,我得到:
Object doesn't support property or method 'transformNode'
对象不支持属性或方法“transformNode”
Any ideas on why Internet Explorer 10 is doing this?
关于为什么 Internet Explorer 10 这样做的任何想法?
EDIT
编辑
I have also tried this:
我也试过这个:
xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.async = false;
xmldoc.load(xml);
xsldoc = new ActiveXObject("Msxml2.DOMDocument");
xsldoc.async = false;
xsldoc.load(xsl);
var content = xmldoc.transformNode(xsldoc);
Which works in all previous versions of IE, but in IE 10 I get:
它适用于所有以前版本的 IE,但在 IE 10 中我得到:
Reference to undeclared namespace prefix: 'atom'.
引用未声明的命名空间前缀:'atom'。
采纳答案by John Chapman
Found the answer: http://blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx
找到答案:http: //blogs.msdn.com/b/ie/archive/2012/07/19/xmlhttprequest-responsexml-in-ie10-release-preview.aspx
IE 10 requires using an XMLHttpRequest with the responseType set as "msxml-document". Once I switched the code over to that, everything works perfectly in all browsers:
IE 10 需要使用 XMLHttpRequest 并将 responseType 设置为“msxml-document”。一旦我将代码切换到那个,一切都在所有浏览器中完美运行:
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP"); // For IE 6
}
xhr.open("GET", url, false);
try { xhr.responseType = "msxml-document"; } catch (e) { };
xhr.send();
回答by The Alpha
IE 9 and grater doesn't support it, try this function (found online)
IE 9 和刨丝器不支持,试试这个功能(网上找的)
function TransformToHtmlText(xmlDoc, xsltDoc) {
if (typeof (XSLTProcessor) != "undefined") { // FF, Safari, Chrome etc
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
var xmlFragment = xsltProcessor.transformToFragment(xmlDoc, document);
return GetXmlStringFromXmlDoc(xmlFragment);
}
if (typeof (xmlDoc.transformNode) != "undefined") { // IE6, IE7, IE8
return xmlDoc.transformNode(xsltDoc);
}
else {
try { // IE9 and grater
if (window.ActiveXObject) {
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
xslDoc.loadXML(xsltDoc.xml);
xslt.stylesheet = xslDoc;
var xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.transform();
return xslProc.output;
}
}
catch (e) {
alert("The type [XSLTProcessor] and the function [XmlDocument.transformNode] are not supported by this browser, can't transform XML document to HTML string!");
return null;
}
}
}
var content = TransformToHtmlText(xml, xsl);
回答by art guy
I had the same problem with IE 9 and none of the answers helped until I stopped trying to load the xslt
file using jQuery
. I loaded the file with a script as documented in: https://msdn.microsoft.com/en-us/library/ms762796%28v=vs.85%29.aspx.
我在 IE 9 上遇到了同样的问题,直到我停止尝试xslt
使用jQuery
. 我使用脚本加载文件,如:https: //msdn.microsoft.com/en-us/library/ms762796%28v=vs.85%29.aspx 中所述。
I was then able to use the transformNode()
function. Here is the script that they gave:
然后我就可以使用该transformNode()
功能了。这是他们提供的脚本:
<HTML>
<HEAD>
<TITLE>sample</TITLE>
<SCRIPT language = "javascript">
function init()
{
var srcTree =
new ActiveXObject("Msxml2.DOMDocument.6.0");
srcTree.async=false;
// You can substitute other XML file names here.
srcTree.load("hello.xml");
var xsltTree =
new ActiveXObject("Msxml2.DOMDocument.6.0");
xsltTree.async = false;
// You can substitute other XSLT file names here.
xsltTree.load("hello.xsl");
resTree.innerHTML = srcTree.transformNode(xsltTree);
}
</SCRIPT>
</HEAD>
<BODY onload = "init()" >
<div id="resTree"></div>
</BODY>
</HTML>
回答by Anthony De Souza
Firstly credit to Roel van Lisdonk who published the function Sheik Heera shared.
首先归功于 Roel van Lisdonk,他发布了 Sheik Heera 共享的功能。
I found this function as it was didn't work in Chrome, because of GetXmlStringFromXmlDoc() so I used the XMLSerializer:
我发现这个函数在 Chrome 中不起作用,因为 GetXmlStringFromXmlDoc() 所以我使用了 XMLSerializer:
So for example:
例如:
if (typeof(GetXmlStringFromXmlDoc)!= "undefined")
{
return GetXmlStringFromXmlDoc(xmlFragment);
}
else
{
// chrome friendly
// get a xml serializer object
var xmls = new XMLSerializer();
// convert dom into string
var sResult = xmls.serializeToString(xmlFragment);
//extract contents of transform iix node if it is present
if (sResult.indexOf("<transformiix:result") > -1)
{
sResult = sResult.substring(sResult.indexOf(">") + 1, sResult.lastIndexOf("<"));
}
return sResult;
}
The revised function is now:
修改后的函数现在是:
function TransformToHtmlText(xmlDoc, xsltDoc)
{
// 1.
if (typeof (XSLTProcessor) != "undefined")
{
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
var xmlFragment = xsltProcessor.transformToFragment(xmlDoc, document);
if (typeof(GetXmlStringFromXmlDoc)!= "undefined")
{
return GetXmlStringFromXmlDoc(xmlFragment);
}
else
{
// chrome friendly
// get a xml serializer object
var xmls = new XMLSerializer();
// convert dom into string
var sResult = xmls.serializeToString(xmlFragment);
//extract contents of transform iix node if it is present
if (sResult.indexOf("<transformiix:result") > -1)
{
sResult = sResult.substring(sResult.indexOf(">") + 1, sResult.lastIndexOf("<"));
}
return sResult;
}
}
// 2.
if (typeof (xmlDoc.transformNode) != "undefined")
{
return xmlDoc.transformNode(xsltDoc);
}
else {
var activeXOb = null;
try { activeXOb = new ActiveXObject("Msxml2.XSLTemplate"); } catch (ex) {}
try {
// 3
if (activeXOb)
{
var xslt = activeXOb;
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
xslDoc.loadXML(xsltDoc.xml);
xslt.stylesheet = xslDoc;
var xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.transform();
return xslProc.output;
}
}
catch (e)
{
// 4
alert("The type [XSLTProcessor] and the function [XmlDocument.transformNode] are not supported by this browser, can't transform XML document to HTML string!");
return null;
}
}
}