Click to See Complete Forum and Search --> : xsl transform as a string in Javascript


sack_it
03-24-2005, 11:34 AM
Is it possible to return the results of an xml/xsl transform as a string using Javascript. I have found XSLTProcessor which provides very useful methods to set parameters in the xsl file which I will need but this doesn't work on IE and earlier versions of Netscape. Here's my code so far

XML File
<?xml version="1.0" ?>
<products category="big stuff">
<item id="123">
<name ref="abc3">Unnamed Item1</name>
<des>C_Description not available</des>
</item>
<item id="456">
<name ref="abc1">Unnamed Item2</name>
<des>B_Description not available</des>
</item>
<item id="789">
<name ref="abc2">Unnamed Item3</name>
<des>A_Description not available</des>
</item>
</products>

Simple XSL File
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="products/item">
<p>
<xsl:value-of select="name"/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Here's the section of my Javascript (at the bottom) that I wish to have the transformation returned as a string. All it does so far is successfully read in the xml file.


function initatepage(itemcategory, startPos) {
myxmlfile = 'xmls/'+itemcategory+'.xml';

if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.addEventListener('load', loadHandler, false);
xmlDoc.load(myxmlfile);
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function() {
if(xmlDoc.readyState == 4) loadHandler()
};
xmlDoc.load(myxmlfile);
}
else
{
alert(getIncompatMsg());
return;
}

function loadHandler () {
// here's where I want to get the transformation as a string
so that I can pass it as a result to another function to add it to a page

} // end loadHandler

} // end initatepage


Hope you can help

Carl

sack_it
03-29-2005, 06:36 AM
I've had another look at the code and managed to get it to a stage where it works on IE 5.0+ and Netscape 7.0+ but I would like it to work on netscape 6. Can anyone help to acheive this? The error message on Netscape 6 is "XSLTProcessor is not defined" so I imagine that class isn't supported. Also does anyone know if the code would work on other browsers?


function initatepage(itemcategory, startPos) {
myxmlfile = 'xmls/'+itemcategory+'.xml';
myxslfile = 'xmls/store.xsl';


if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.addEventListener('load', xslLoadHandler, false);
xmlDoc.load(myxmlfile);

} else if (window.ActiveXObject){

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function() {
if(xmlDoc.readyState == 4) xslLoadHandler()
};
xmlDoc.load(myxmlfile);
} else {
alert(getIncompatMsg());
return;
}

function xslLoadHandler() {

if (document.implementation && document.implementation.createDocument) {
xslDoc = document.implementation.createDocument('', '', null);
xslDoc.addEventListener('load', netscapeloadHandler, false);
xslDoc.load(myxslfile);

} else if (window.ActiveXObject){

xslDoc = new ActiveXObject("Microsoft.XMLDOM");
xslDoc.onreadystatechange = function() {
if(xslDoc.readyState == 4) ieloadHandler()
};
xslDoc.load(myxslfile);
} else {
alert(getIncompatMsg());
return;
}

} // end xslLoadHandler

function netscapeloadHandler () {
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
var myFragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById('pointToInsertProducts').appendChild(myFragment);
} // end loadHandler

function ieloadHandler () {

var xslString = xmlDoc.transformNode(xslDoc);
document.getElementById('pointToInsertProducts').innerHTML=xslString;

} // end loadHandler
} // end initatepage


Thanks in advance

Carl