Jim
Jim

Reputation: 13

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?

Upvotes: 1

Views: 6053

Answers (3)

matyr
matyr

Reputation: 5774

If supported, data URL can be handy.

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

Upvotes: 1

user357812
user357812

Reputation:

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>

Upvotes: 2

Cristian Sanchez
Cristian Sanchez

Reputation: 32107

Setting the doctype for the output: http://www.bernzilla.com/item.php?id=763

Upvotes: 0

Related Questions