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