View Full Version : How to change dynamically XSL in a XML or call with Javascript?
SickMothaF
07-25-2006, 12:53 PM
Hello, I need to change the XSL file of a XML file by call for example.
OR can I call a Javascript function which tells the xml which xsl to use and display the xml with the new xsl ???
SickMothaF
07-26-2006, 07:43 AM
you guys don't know much , do you? :eek:
Alex Vincent
07-26-2006, 08:40 AM
That's not fair. Some people know the answers to certain areas, others know to other areas, and on some questions it's just so obscure no one has the answer.
I thought about answering earlier, based on my understanding, but I wasn't too sure I was right. So, here goes.
The problem with XSLT (which is what I think you mean here) is that it literally transforms the original document into something else, destroying the original in the process.
JS within XML would execute after the XSLT for the same document has transformed it. That approach is therefore impossible.
You could probably do something like this server-side, before the XSL stylesheet is called. Or, if you have a wrapper document (say, the XSL is in another frame or window), there may be ways, depending on the browser and on the file serving mechanisms, to do this by script.
SickMothaF
07-26-2006, 09:33 AM
Yea, you are right, but I also saw that there are too many unsolved posts in this forum... And you are may be one of the few answering...
I saw somewhere this script , but it did not worked for me? Any ideas?
// Load data.
//var divInfo='';
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.load("XMLdata.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
// Load style sheet.
var xslDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
if (xslDoc.parseError.errorCode != 0) {
var myErr = xslDoc.parseError;
alert("You have error " + myErr.reason);
} else {
xslDoc.async = false
xslDoc.load("XSLdata.xsl");
alert(xmlDoc.transformNode(xslDoc));
// Fill a div tag with the result of the transform
divInfo.innerHTML = xmlDoc.transformNode(xslDoc);
}
}
SickMothaF
07-26-2006, 10:36 AM
Problem solved!!! :thumbsup:
Except that it only works in IE
ANY Ideas how to make it work on Firefox ?
<html>
<body>
<script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("XMLdata.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("XSLdata.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.