如何在Mozilla Gecko 用Javascript載入XSL

來源:互聯網
上載者:User

在Mozilla Develop Center裡,我們可以看到有以下的文章:http://developer.mozilla.org/en/docs/The_XSLT/JavaScript_Interface_in_Gecko:Basic_Example
首先,你需要瞭解如何動態載入xml檔案的方法,可以用XMLDOM對象,也可以用XMLHttpRequest,的responseXML對象,這裡我用的是XMLHttpRequest。

用javascript載入xslt的方法如下:
1。用XMLDOM或者用XMLHttpRequest來載入xml和xslt。
2。用XSLTProcessor.importStylesheet來引入XSLT。
3。用XSLTProcessor.transformToFragment方法來把它轉換成DOM的Fragment。然後用appendChild或者用insertBefore等方法來追加或者插入這個DOM的fragment元素。
範例程式碼
var ownerDocument = document.implementation.createDocument("", "test", null);
var newFragment = processor.transformToFragment(domToBeTransformed, ownerDocument);
當然也可以用transformToDocument
var newDocument = processor.transformToDocument(domToBeTransformed);
需要注意的是,轉換後的節點是Element或者是一個片段,所以要經過下面的序列化才可使用obj.innerHTML=new Document
4。序列化。
(new XMLSerializer()).serializeToString(newDocument)
5。在IE中,可以用XMLDOM方法,xmldoc.transformNode(xslDocument)方法來進行接的轉換。

首先,我們先建立一個XML檔案與XSLT檔案,方便後面的講解。
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="/">
<h1 class="title"><xsl:value-of select="/Article/Title"/></h1>
<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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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, "調用XMLHTTP錯誤,沒有指定檔案名稱。"]);
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, "調用XMLHTTP錯誤,遠程檔案失敗。"+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);//序列化
};
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>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.