I recently used Firefox to debug web pages and found that some Javascript XSLT statements for processing XML only support IE browsers. Some articles on the Internet that introduce JavaScript XSLT to process XML are basically based on AJAX.
In desperation, I wrote a small function of Javascript XSLT to process XML display page. Now I'm posting it to share with everyone, and I hope you can give me some suggestions for improvement.
When using the XSLTProcessor object to process XML in Firefox, you mainly use two methods of this object:
1. transformToFragment().
2. transformToDocument().
The following code only uses the transformToFragment() method to process XML files. If you are interested in using Javascript XSLT to process XML files in Firefox, you might as well try to rewrite the following code to use the transformToDocument() method to achieve the processing function.
The Javascript code is as follows:
function initialize() {
var xmlDoc;
var xslDoc;
// Determine the browser type
if(document.implementation && document.implementation.createDocument)
{
// Support Mozilla browser
try
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load("guestbook/guestbook.xml");
}
catch(e)
{
alert("error:001");
}
try
{
xslDoc = document.implementation.createDocument("", "", null);
xslDoc.async = false;
xslDoc.load("guestbook/guestbook.xsl");
}
catch(e)
{
alert("error:002");
}
try
{
//Define XSLTProcessor object
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
var oResultFragment = xsltProcessor.transformToFragment(xmlDoc,document);
// Output the parsed text to the page
var oDiv = document.getElementById("guestbookPanel");
oDiv.appendChild(oResultFragment);
}
catch(e)
{
alert("error:003");
}
}
else if(typeof window.ActiveXObject != 'undefined')
{
//var xmlDoc=Server.CreateObject("Msxml2.DOMDocument.4.0");
//Support IE browser
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xslDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xslDoc.async = false;
xmlDoc.load("guestbook/guestbook.xml");
xslDoc.load("guestbook/guestbook.xsl");
guestbookPanel.innerHTML = xmlDoc.documentElement.transformNode(xslDoc);
}
else
{
alert("Browser unknown!");
}
}
javascript dom is the second way to handle XSL display data.
The main code is as follows:
var xmlDoc;
var xslDoc;
// Determine the browser type
if(document.implementation && document.implementation.createDocument)
{
// Support Mozilla browser
try
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load("guestbook/guestbook.xml");
xslDoc = document.implementation.createDocument("", "", null);
xslDoc.async = false;
xslDoc.load("guestbook/guestbook.xsl");
//Define XSLTProcessor object
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
// transformToDocument method
var result = xsltProcessor.transformToDocument(xmlDoc);
var xmls = new XMLSerializer();
document.getElementById("guestbookPanel").innerHTML = xmls.serializeToString(result);
}
catch(e)
{
alert("Unable to do xml/xsl processing");
}
}
else if(typeof window.ActiveXObject != 'undefined')
{
try
{
//Support IE browser
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xslDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async = false;
xslDoc.async = false;
xmlDoc.load("guestbook/guestbook.xml");
xslDoc.load("guestbook/guestbook.xsl");
guestbookPanel.innerHTML = xmlDoc.documentElement.transformNode(xslDoc);
}
catch(e)
{
alert("Unable to do xml/xsl processing");
}
}
else
{
alert("Browser unknown!");
}