views:

26

answers:

3

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?

A: 

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

CD Sanchez
the Doctype is already set in the xslt
Jim
A: 

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>
Alejandro
A variation on this did the trick. Many thanks!
Jim
A: 

If supported, data URL can be handy.

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