The recent use of Firefox for Web page debugging, found that some JavaScript XSLT processing XML statements only support IE browser. Some of the articles in the web that introduce JavaScript XSLT to deal with XML are basically based on Ajax.
Helpless, I wrote a JavaScript XSLT processing XML to show the small features of the page. Now post out and share with you, I hope you give some suggestions for improvement.
Using the Xsltprocessor object to process XML in Firefox, there are two main ways to use the object:
One, Transformtofragment ().
Second, transformtodocument ().
The following code simply uses the Transformtofragment () method to implement the XML file processing if you are using JavaScript XSLT in Firefox If you are interested in working with an XML file, try rewriting the following code into a processing function that is implemented using the Transformtodocument () method.
The Javascript code is as follows:
function Initialize () {
var xmldoc;
var xsldoc;
Determine the type of browser
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
{
Defining Xsltprocessor Objects
var xsltprocessor = new Xsltprocessor ();
Xsltprocessor.importstylesheet (Xsldoc);
var oresultfragment = xsltprocessor.transformtofragment (xmldoc,document);
Output 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!");
}
}
The JavaScript DOM handles the second way that XSL displays data.
The main code is as follows:
var xmldoc;
var xsldoc;
Determine the type of browser
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");
Defining Xsltprocessor Objects
var xsltprocessor = new Xsltprocessor ();
Xsltprocessor.importstylesheet (Xsldoc);
Transformtodocument Way
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!");
}
How to use JavaScript XSLT to process XML files (Firefox supported)