Posted on 18/07/2008 10:26:01
I posted a javascript xml/xslt transformation about a year ago in this forum. But unfortunately it seems to be deleted. Anyways, i've lookup the code again and here it is, and u can put it into the html code of a Dynamicweb paragraph:
(replace the XML and XSLT to use in the bottom of this code) *sorry this looks so bad, but couldn't find a nicer way to display the code*
<div id="PlaceHolder1">this is replaced by the transformation</div>
<script type="text/javascript" language="javascript">
function getDomFromFile(filename) {
// Load XML
var xml;
if (typeof ActiveXObject != 'undefined') {// IE
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load(filename);
}
else { // others
var myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", filename, false);
myXMLHTTPRequest.send(null);
xml = myXMLHTTPRequest.responseXML;
}
return xml;
}
function getDomFromXml(xml) {
var dom;
if (typeof ActiveXObject != 'undefined') {
dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
dom.loadXML(xml);
}
else {
parser = new DOMParser();
dom = parser.parseFromString(xml, "text/xml");
}
return dom;
}
function xslt(xmlDoc,xslDoc) {
var transform;
if (typeof ActiveXObject != 'undefined') { //IE
transform = xmlDoc.transformNode(xslDoc);
}
else { //mozilla
var xsl = new XSLTProcessor();
xsl.importStylesheet(xslDoc);
var fragment=xsl.transformToFragment(xmlDoc, document);
if( fragment.childNodes.length>0 )
transform = fragment.childNodes[0].innerHTML;
else
return "error during transformation";
}
return transform;
}
// get the dom of the xml file
var xmldoc = getDomFromFile( "/Files/Filer/demo.xml" );
// get the dom of the xsl file
var xsldoc = getDomFromFile( "/Files/Filer/demo.xsl" );
// do the transformation and put response in placeholder
document.getElementById("PlaceHolder1").innerHTML = xslt(xmldoc, xsldoc);
</script>