Home Web Front-end JS Tutorial How to load XSL with Javascript in Mozilla Gecko_javascript tips

How to load XSL with Javascript in Mozilla Gecko_javascript tips

May 16, 2016 pm 07:21 PM

In Mozilla Develop Center, we can see the following article: http://developer.mozilla.org/en/docs/The_XSLT/JavaScript_Interface_in_Gecko:Basic_Example
First of all, you need to understand how to dynamically load xml files , you can use XMLDOM object, you can also use XMLHttpRequest, the responseXML object, here I use XMLHttpRequest.

The method of loading xslt using javascript is as follows:
1. Use XMLDOM or XMLHttpRequest to load xml and xslt.
2. Use XSLTProcessor.importStylesheet to introduce XSLT.
3. Use the XSLTProcessor.transformToFragment method to convert it into a DOM Fragment. Then use appendChild or insertBefore and other methods to append or insert the fragment element of this DOM.
Example code
var ownerDocument = document.implementation.createDocument("", "test", null);
var newFragment = processor.transformToFragment(domToBeTransformed, ownerDocument);
Of course you can also use transformToDocument
var newDocument = processor.transformToDocument(domToBeTransformed);
It should be noted that the converted node is an Element or a fragment, so it must be serialized as follows before using obj.innerHTML=new Document
4. serialization.
(new XMLSerializer()).serializeToString(newDocument)
5. In IE, you can use the XMLDOM method and xmldoc.transformNode(xslDocument) method to perform direct conversion.

First, we create an XML file and XSLT file to facilitate the explanation later.
foo.xml


javascript load xslt in ie and mozilla
never-online
http://www.never-online.net
content is here

foo.xsl





foo.html

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/tr/html4/strict.dtd">


convert xsl using javascript - http://www.never-online.net






.title { margin :10px 10% 0 10%; text-align:center; background-color:#639ACE; padding:10px; color:#fff; }
.desc { margin:10px 10% 0 10%; text-align: center; }
.box { margin:10px 10% 0 10%; border: 1px dotted #639ACE; padding:20px; }




<script> <BR> //<![CDATA[ <br><br> //]]> <BR> </script> <script> <BR>//<![CDATA[ <BR> var xsltParser = function(xmlfileStr, xslfileStr) { <BR> var retval = xslStylesheet = xmlDocument = null; <BR> var browser = { <BR> isIE:!!window.ActiveXObject, <BR> isMozilla:(typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined') <BR> }; <BR> var loadDocument = function (fileStr) { <BR> if (!fileStr) throw new Error([65221, "调用XMLHTTP错误,没有指定文件名。"]); <BR> var req = browser.isIE?new ActiveXObject("MSXML2.XMLHTTP"):new XMLHttpRequest(); <BR> req.open("GET", fileStr, false); <BR> req.send(null); <BR> if (req.readyState==4 && req.status==200) { return req.responseXML; } <BR> else throw new Error([65222, "调用XMLHTTP错误,远程文件失败。"+fileStr+""]); <BR> }; <BR> var ready2Transform = function () { <BR> xmlDocument = loadDocument(xmlfileStr); <BR> xslStylesheet = loadDocument(xslfileStr); <BR> }(); <BR> var parseFromMoz = function () { <BR> var xsltProcessor = new XSLTProcessor(); <BR> xsltProcessor.importStylesheet(xslStylesheet); <BR> var retval = xsltProcessor.transformToDocument(xmlDocument); <BR> return (new XMLSerializer()).serializeToString(retval);//序列化 <BR> }; <BR> var parseFromIE = function () { <BR> return xmlDocument.transformNode(xslStylesheet.documentElement); <BR> }; <BR> if (browser.isMozilla) { <BR> retval = parseFromMoz(xmlfileStr, xslfileStr); <BR> } <BR> else if (browser.isIE) { <BR> retval = parseFromIE(xmlfileStr, xslfileStr); <BR> } else { /* TO DO */ ;}; return retval; <BR> } <BR> document.getElementById("demo").innerHTML=xsltParser("foo.xml","foo.xsl") <BR>//]]> <BR></script>
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles