How to use JavaScript XSLT to process XML files (Firefox supported)

Source: Internet
Author: User
Tags xsl xslt

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.