Recently using Firefox for Web page debugging, I found that some JavaScript XSLT processing XML statements only support IE browsers. Some of the articles in the web that describe JavaScript XSLT processing XML are basically based on Ajax.
In desperation, I wrote a JavaScript XSLT to handle the small functionality of the XML Presentation page. Now posted to share with you, I hope we can give some suggestions for improvement.
Using the Xsltprocessor object in Firefox to process XML, you use the object's two methods primarily:
One, Transformtofragment ().
Second, transformtodocument ().
The following code only uses the Transformtofragment () method to handle XML files, 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 implemented using the Transformtodocument () method.
The Javascript code is as follows:
Copy Code code 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 a 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 in which XSL displays data.
The main code is as follows:
Copy Code code 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!");
}