problem with Transforming dynamically generated xmll

R

reed.emmons

I'm trying to load and transform dynamically created xml. It works
great in IE but in Mozilla, it will not transform. I discovered that
when I copy the generated xml into a flat file, it works.

It might be caused by the function that processes the XML was called
before the xml document was completely loaded, though I don't know how
to resolve that.

Any ideas?

<html>
<head>
<script>
function loadXMLDoc(fileName)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject){
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation &&
document.implementation.createDocument){
xmlDoc=document.implementation.createDocument("","",null);
}

xmlDoc.async=false;
xmlDoc.load(fileName);

return(xmlDoc);
}

function loadInventory()
{
xml=loadXMLDoc("/inventory/inventoryXML");
xsl=loadXMLDoc("/xsl/inventory.xsl");

// code for IE
if (window.ActiveXObject){
ex=xml.transformNode(xsl);
document.getElementById("inventory").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("inventory").appendChild(resultDocument);
}
}
</script>
</head>
<body onLoad="loadInventory()">
<div id="inventory"></div>
</body>
</html>
 
M

Martin Honnen

I'm trying to load and transform dynamically created xml. It works
great in IE but in Mozilla, it will not transform. I discovered that
when I copy the generated xml into a flat file, it works.

You will need to show a minimal but complete sample of the XML and XSLT
demonstrating the problem. Make sure that the server sends the HTTP
Content-Type: application/xml header for your XML as otherwise Mozilla
does not build an XML DOM document at all.

function loadXMLDoc(fileName)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject){
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

Note that only Msxml2.DOMDocument.3.0 and later (e.g.
Msxml2.DOMDocument.4.0, 5.0, 6.0) support XSLT 1.0 while
Microsoft.XMLDOM might give you a DOM document that does not support
XSLT 1.0.
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument){

If you want to use XSLTProcessor then why do you check for other
unrelated objects? I strongly suggest to check
if (typeof XSLTProcessor != 'undefined')
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("inventory").appendChild(resultDocument);
}

I don't see anything wrong with the code that would cause it to fail, as
said, show us the XML and XSLT and we can tell you more.
 
R

reed.emmons

Hi and thank you for the follow-up.

xml sample
------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<inventory>
<wine>
<inventory_wineId>5</inventory_wineId>
<wineId>16</wineId>
<wineryName>Cardeal</wineryName>
<wine_alternateName>Reserva</wine_alternateName>
<styleName>Still Wine</styleName>
<colorName>Red</colorName>
<countryName>Portugal</countryName>
<regionName>Dão</regionName>
<vintageYear>2001</vintageYear>
<quantityPurchased>12</quantityPurchased>
<pricePerUnit>11.66</pricePerUnit>
<organizationName>The Winery</organizationName>
<ranking>4</ranking>
<dt_Purchased>2007-11-20 00:00:00</dt_Purchased>
</wine>
</inventory>


inventory.xsl
------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th align="left">Winery</th>
<th align="left">Vintage</th>
<th align="left">Color</th>
<th align="left">Style</th>
</tr>
<xsl:for-each select="inventory/wine">
<tr>
<td><xsl:value-of select="wineryName" /></td>
<td><xsl:value-of select="vintageYear" /></td>
<td><xsl:value-of select="colorName" /></td>
<td><xsl:value-of select="styleName" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
 
M

Martin Honnen

Hi and thank you for the follow-up.

Have you checked that your application sends the HTTP
Content-Type: application/xml
header for the dynamically generated XML? That is necessary with Mozilla
to build a DOM with the load method.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

As you want to generate a HTML fragment to be included into a HTML div
element I suggest to set
<xsl:template match="/">
<html>
<body>
^^^^^^
Throw them out, it does not make sense to have them for your case where
the result of the transformation is a fragment to be inserted into a div.
 
R

reed.emmons

Ouch, of course, I didn't set the Content-Type.

Thanks a million; who knows when I would have figured that one out!
Cheers,
Reed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top