Javascript XSLT compatible with Firefox processes XML files and javastxml

Source: Internet
Author: User
Tags xslt

Javascript XSLT compatible with Firefox processes XML files and javastxml

Recently, I used Firefox for web page debugging and found that some Javascript XSLT statements that process XML only support the IE browser. Some articles on the web that introduce javascript XSLT to process XML are basically based on AJAX.

In desperation, I wrote a small Javascript XSLT function to process XML display pages. I hope you can give me some suggestions for improvement.
In Firefox, The tprocessor object is used to process XML, mainly using two methods of this object:
1. transformToFragment ().

Ii. 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, try to rewrite the following code into a processing function implemented using the transformToDocument () method.
The Javascript code is as follows:
 
Copy codeThe Code is as follows:
Function initialize (){
Var xmlDoc;
Var callback Doc;

// Determine the browser type
If (document. implementation & document. implementation. createDocument)
{
// Supports Mozilla browsers
Try
{
XmlDoc = document. implementation. createDocument ("", "", null );
XmlDoc. async = false;
XmlDoc. load ("guestbook/guestbook. xml ");
}
Catch (e)
{
Alert ("error: 001 ");
}
Try
{
Required Doc = document. implementation. createDocument ("", "", null );
Optional Doc. async = false;
Using Doc. load ("guestbook/guestbook. xsl ");

}
Catch (e)
{
Alert ("error: 002 ");
}
Try
{
// Define a tprocessor object
Var effectprocessor = new effectprocessor ();
Effectprocessor. importStylesheet (invalid DOC );
Var oResultFragment = effectprocessor. 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 ");
// Supports Internet Explorer
XmlDoc = new ActiveXObject ('Microsoft. XMLDOM ');
Export Doc = new ActiveXObject ('Microsoft. XMLDOM ');
XmlDoc. async = false;
Optional Doc. async = false;
XmlDoc. load ("guestbook/guestbook. xml ");
Using Doc. load ("guestbook/guestbook. xsl ");
GuestbookPanel. innerHTML = xmlDoc.doc umentElement. transformNode (invalid DOC );
}
Else
{
Alert ("Browser unknown! ");
}
}

The second method for processing XSL display data using javascript dom.

The main code is as follows:

Copy codeThe Code is as follows:
Var xmlDoc;
Var callback Doc;
// Determine the browser type
If (document. implementation & document. implementation. createDocument)
{
// Supports Mozilla browsers
Try
{
XmlDoc = document. implementation. createDocument ("", "", null );
XmlDoc. async = false;
XmlDoc. load ("guestbook/guestbook. xml ");
Required Doc = document. implementation. createDocument ("", "", null );
Optional Doc. async = false;
Using Doc. load ("guestbook/guestbook. xsl ");
// Define a tprocessor object
Var effectprocessor = new effectprocessor ();
Effectprocessor. importStylesheet (invalid DOC );
// TransformToDocument Method
Var result = effectprocessor. 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
{
// Supports Internet Explorer
XmlDoc = new ActiveXObject ('msxml2. domdocument ');
Required Doc = new ActiveXObject ('msxml2. domdocument ');
XmlDoc. async = false;
Optional Doc. async = false;
XmlDoc. load ("guestbook/guestbook. xml ");
Using Doc. load ("guestbook/guestbook. xsl ");
GuestbookPanel. innerHTML = xmlDoc.doc umentElement. transformNode (invalid DOC );
}
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.