This article describes how to use JavascriptXSLT compatible with Firefox to process XML files. For more information about how to use Firefox to debug webpages, see this article. it is found that some Javascript XSLT statements for XML processing only support IE browsers. 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:
The 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:
The 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! ");
}