简体   繁体   中英

How to load XML (with xslt) generated by javascript into an IFRAME?

I'm generating some simple XML via javascript, and then using doc.open, doc.write, and doc.close to write the xml into an iframe.

My problem is that in the iframe, it is not being rendered properly. It's as if the xslt renderer is not kicking into gear and it tries to render as html(just showing the text node values).

The xml itself is proper and when pasted in an xml file and loaded, renders properly with the xslt.

Is it a matter of somehow telling the browser what data type the generated xml is (and how would i do that?) or is there a way to kick it into xslt rendering mode?

I think that the best approach it would be to run the transformation with javascript and then to add result to DOM. As example, from http://www.w3schools.com/xsl/xsl_client.asp

<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml=loadXMLDoc("cdcatalog.xml");
xsl=loadXMLDoc("cdcatalog.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);
  }
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>

如果支持,则数据URL会很方便。

iframe.src = 'data:text/xml,' + encodeURI('<x m="l"/>');

设置输出的文档类型: http : //www.bernzilla.com/item.php?id= 763

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM