Home > Web Front-end > JS Tutorial > Firefox compatible Javascript XSLT processing XML files_javascript tips

Firefox compatible Javascript XSLT processing XML files_javascript tips

WBOY
Release: 2016-05-16 16:22:54
Original
1768 people have browsed it

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:

Copy code The code is as follows:

function initialize() {
    var xmlDoc;
    var xslDoc;

    // 判断浏览器的类型
    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:

Copy code The code is as follows:

var xmlDoc;
var xslDoc;
// Determine the browser type
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");
// Define XSLTProcessor object
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslDoc);
// transformToDocument method
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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template