使用JavaScript在IE中加载XML/XSLT
我正在构建一个外观整洁的日志系统,灵感来自。我的一般设置如下:使用JavaScript在IE中加载XML/XSLT,javascript,xml,internet-explorer,xslt,logging,Javascript,Xml,Internet Explorer,Xslt,Logging,我正在构建一个外观整洁的日志系统,灵感来自。我的一般设置如下: <?xml version="1.0"?> <RunTimeLog> <LogHeader> <StartDate>02-09-2013</StartDate> <StartTime>09:52</StartTime> <Platform>Windows 8</Platform> <M
<?xml version="1.0"?>
<RunTimeLog>
<LogHeader>
<StartDate>02-09-2013</StartDate>
<StartTime>09:52</StartTime>
<Platform>Windows 8</Platform>
<MemoryTotal>1,048,576 kB</MemoryTotal>
</LogHeader>
<LogEvents>
<LogEvent id="1">
<Type>Warning</Type>
<TimeIndex>00:01:34</TimeIndex>
<File>application.cc</File>
<Function>SomeFunction()</Function>
<LineNumber>314</LineNumber>
<Message>This is just a randum warning, nothing to worry about!</Message>
</LogEvent>
<LogEvent id="2">
<Type>Debug</Type>
<TimeIndex>00:01:34</TimeIndex>
<File>application.cc</File>
<Function>SomeFunction()</Function>
<LineNumber>314</LineNumber>
<Message>This is just a randum warning, nothing to worry about!</Message>
</LogEvent>
</LogEvents>
</RunTimeLog>
function loadXMLDoc(dname) {
if (window.ActiveXObject) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", dname, false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult(name) {
if(name == null)
xml = loadXMLDoc("testlog.xml");
else
xml = loadXMLDoc(name);
xsl = loadXMLDoc("layout.xsl");
document.getElementById("logview").innerHTML = "";
// code for IE
if (window.ActiveXObject) {
ex = xml.transformNode(xsl);
document.getElementById("logview").innerHTML = ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("logview").appendChild(resultDocument);
}
其作用:当用户选择要加载的本地文件并传递本地文件的名称时,将调用displayResult()。然后,脚本将尝试从光盘加载XML文件和XSL文件,并将XML转换为HTML,然后再使用新的HTML替换DIV的内容
我的XML如下所示:
<?xml version="1.0"?>
<RunTimeLog>
<LogHeader>
<StartDate>02-09-2013</StartDate>
<StartTime>09:52</StartTime>
<Platform>Windows 8</Platform>
<MemoryTotal>1,048,576 kB</MemoryTotal>
</LogHeader>
<LogEvents>
<LogEvent id="1">
<Type>Warning</Type>
<TimeIndex>00:01:34</TimeIndex>
<File>application.cc</File>
<Function>SomeFunction()</Function>
<LineNumber>314</LineNumber>
<Message>This is just a randum warning, nothing to worry about!</Message>
</LogEvent>
<LogEvent id="2">
<Type>Debug</Type>
<TimeIndex>00:01:34</TimeIndex>
<File>application.cc</File>
<Function>SomeFunction()</Function>
<LineNumber>314</LineNumber>
<Message>This is just a randum warning, nothing to worry about!</Message>
</LogEvent>
</LogEvents>
</RunTimeLog>
带着错误
SCRIPT16389: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.
我尝试过其他线程和论坛中给出的多种解决方案:更改MIME类型,使用不同的根节点,甚至用不同的文档替换XML和XSLT,这些都是100%确定的。另外,如果我只是在IE中使用href来使用XML和XSLT,它将显示为没有问题。这让我觉得问题在于ActiveXObject的加载功能。这里有没有人能说明问题所在
提前感谢,
Yuri很好地调试代码,检查
console.log(xsl.parseError.errorCode)
,console.log(xsl.parseError.reason)
,console.log(xsl.documentElement!==null)
。你使用哪个版本的IE?XSLT文档的MIME类型是什么?您的代码是反向的。如果存在本机XmlHttpRequest对象,则应使用该对象,如果没有,则仅尝试返回ActiveX对象。@如果我反转if语句,它会在下面的“xhttp.open(“GET”,dname,false);”语句中告诉我“访问被拒绝”。@MartinHonnen我使用的是10.0.8版本的IE,并且在兼容模式下也尝试了旧版本。我目前没有以某种MIME类型显式提供服务。一切都是本地的(我希望保持这种状态)。控制台日志分别给我0、“、和false,这意味着加载失败,对吗?看到xsl.documentElement为空吗?如果您使用从文件系统加载的HTML文档,但启用了脚本和ActiveX,那么对于IE分支,我只需使用If(window.ActiveXObject)然后{var doc=new ActiveXObject('Msxml2.DOMDocument.6.0');doc.async=false;doc.load(dname);If(doc.parserror.errorCode==0){return doc;}否则{//需要在这里处理解析错误}否则{//其他浏览器的代码在这里}
。
SCRIPT16389: The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.