Firefox-compatible JavaScript XSLT handles XML file _javascript techniques

Source: Internet
Author: User
Tags xsl xslt

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!");
}

Related Article

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.