I recently used Firefox to debug web pages and found that some Javascript XSLT statements for processing XML only support IE browsers. Some articles on the Internet that introduce JavaScript XSLT to process XML are basically based on AJAX.
In desperation, I wrote a small function of Javascript XSLT to process XML display page. Now I'm posting it to share with everyone, and I hope you can give me some suggestions for improvement.
Use the XSLTProcessor object to process XML in Firefox, mainly using the two methods of this object:
1. transformToFragment().
2. 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, you might as well try to rewrite the following code to use the transformToDocument() method. processing functions.
The Javascript code is as follows:
// 判断浏览器的类型
if(document.implementation && document.implementation.createDocument)
{
// 支持Mozilla浏览器
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
{
// 定义XSLTProcessor对象
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
var oResultFragment = xsltProcessor.transformToFragment(xmlDoc,document);
// 将解析过的文本输出到页面
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");
// 支持IE浏览器
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!");
}
}
javascript dom is the second way to process XSL display data.
The main code is as follows: