Guy*_*Guy 3 javascript xslt google-chrome
我正在使用以下javascript代码来显示xml/xsl:
function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation
&& document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
try {
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
catch(e)
{
try //Google Chrome
{
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",file,false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML.documentElement;
return(xmlDoc);
}
catch(e)
{
error=e.message;
}
}
}
function displayResult()
{
xml=loadXMLDoc("report.xml");
xsl=loadXMLDoc("report.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("example").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("example").appendChild(resultDocument);
}
}
Run Code Online (Sandbox Code Playgroud)
它适用于IE和Firefox,但Chrome在行中失败:
document.getElementById("example").appendChild(resultDocument);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
谷歌浏览器目前对XSL的支持有限.如果您的XSL是指任何外部资源(document()
函数xsl:import
,xsl:include
或外部实体),该XSL将运行,但结果要么是空的或无效的.
如果这不是您的问题的原因,则问题可能不同:resultDocument
并且document
不共享相同的根.尝试使用importNode或类似的技术.如果失败并且您确定resultDocument具有有效内容,请将其转换为字符串,手动添加节点,并将innerHTML
新节点设置为字符串.我知道,它很难看,但是在Chrome变得成熟之前我们需要一些解决方法......