How to load xsl_javascript techniques with JavaScript in Mozilla Gecko

Source: Internet
Author: User
Tags cdata serialization xmlns xsl xslt
In Mozilla develop center, we can see the following articles: Http://developer.mozilla.org/en/docs/The_XSLT/JavaScript_Interface_in_ Gecko:basic_example
First, you need to know how to dynamically load XML files, either with XMLDOM objects or with XMLHttpRequest, Responsexml objects, where I use XMLHttpRequest.

The way to load XSLT in 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 DOM fragment. Then append or insert the fragment element of the DOM using AppendChild or InsertBefore.
Sample code
var ownerdocument = document.implementation.createDocument ("", "test", null);
var newfragment = processor.transformtofragment (domtobetransformed, ownerdocument);
Of course, you can use Transformtodocument.
var newdocument = processor.transformtodocument (domtobetransformed);
It should be noted that the converted node is an element or a fragment, so you can use the Obj.innerhtml=new Document by following the serialization
4. Serialization.
(New XMLSerializer ()). Serializetostring (NewDocument)
5. In IE, you can use the Xmldom method, Xmldoc.transformnode (Xsldocument) method to carry on the conversion.

First, we first create an XML file and an XSLT file to facilitate the explanation later.
Foo.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<Article>
<title>javascript load XSLT in IE and mozilla</title>
<Author>never-online</Author>
<Web>http://www.never-online.net</Web>
<body>content is here</body>
</Article>
Foo.xsl
<?xml version= "1.0" encoding= "Utf-8"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "html"/>
<xsl:template match= "/" >

<div class= "desc" >author: <xsl:value-of select= "/article/author"/>-
Web: <xsl:value-of select= "/article/web"/></div>
<p class= "box" >
<xsl:value-of select= "/article/body"/>
</p>
</xsl:template>
</xsl:stylesheet>
Foo.html
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >
<title> convert xsl using javascript-http://www.never-online.net </title>
<meta http-equiv= "Imagetoolbar" content= "no"/>
<meta name= "Author" content= "Never-online, Bluedestiny"/>
<meta name= "keywords" content= "never modules, Mozilla CSS, C #,. NET, Refercence, Bluedestiny, Never-online"/>
<meta name= "description" content= "Bluedestiny, Never-online"/>
<meta name= "title" Content= "-http://www.never-online.net"/>
<meta name= "Creator.name" content= "Never-online, Bluedestiny"/>
<style type= "Text/css" media= "All" title= "Default" >
. 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;}
</style>
<script type= "Text/javascript" >
<! [cdata[

]]>
</script>
<body id= "Www.never-online.net" >
<div id= "Demo" ></div>
<script type= "Text/javascript" >
<! [cdata[
var xsltparser = function (Xmlfilestr, xslfilestr) {
var retval = XSLStyleSheet = XmlDocument = null;
var browser = {
isie:!! Window. ActiveXObject,
Ismozilla: (typeof document.implementation!= ' undefined ') && (typeof Document.implementation.createDocument!) = ' undefined ') && (typeof htmldocument!= ' undefined ')
};
var loaddocument = function (filestr) {
if (!FILESTR) throw new error ([65221, "Calling XMLHTTP error, no filename specified. "]);
var req = browser.isie?new ActiveXObject ("MSXML2. XMLHTTP "): New XMLHttpRequest ();
Req.open ("Get", Filestr, false);
Req.send (NULL);
if (req.readystate==4 && req.status==200) {return req.responsexml;}
else throw new error ([65222, "Calls XMLHTTP error, remote file fails.) "+filestr+"]);
};
var ready2transform = function () {
XmlDocument = Loaddocument (XMLFILESTR);
XSLStyleSheet = Loaddocument (XSLFILESTR);
}();
var Parsefrommoz = function () {
var xsltprocessor = new Xsltprocessor ();
Xsltprocessor.importstylesheet (XSLStyleSheet);
var retval = xsltprocessor.transformtodocument (XmlDocument);
Return (new XMLSerializer ()). Serializetostring (retval);//serialization
};
var parsefromie = function () {
Return Xmldocument.transformnode (xslstylesheet.documentelement);
};
if (Browser.ismozilla) {
retval = Parsefrommoz (Xmlfilestr, XSLFILESTR);
}
else if (Browser.isie) {
retval = Parsefromie (Xmlfilestr, XSLFILESTR);
else {/* to do */; return retval;
}
document.getElementById ("Demo"). Innerhtml=xsltparser ("Foo.xml", "foo.xsl")
]]>
</script>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.