如何使用 JavaScript XSLT 處理 XML 文件
最近使用Firefox進行網(wǎng)頁的調(diào)試,發(fā)現(xiàn)有些Javascript XSLT處理XML的語句僅僅支持IE瀏覽器。網(wǎng)絡(luò)中的一些介紹javascript XSLT 處理XML的文章基本上都是依據(jù)AJAX來做的。
寫了一個Javascript XSLT處理XML展現(xiàn)頁面的小功能?,F(xiàn)在帖出來和大家共享,希望大家給點改進意見。
在Firefox中使用XSLTProcessor對象處理XML,主要使用該對象的兩個方法:
一、transformToFragment()。
二、transformToDocument()。
下面的代碼僅僅使用transformToFragment()方法來實現(xiàn)對XML文件處理,如果你對在Firefox中使用 Javascript XSLT 處理XML文件感興趣的話不妨試著將以下代碼改寫成使用transformToDocument()方法來實現(xiàn)的處理功能。
Javascript 代碼如下:
- 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 處理XSL顯示數(shù)據(jù)的第二種方式,主要代碼如下:
- 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");
- xslDoc = document.implementation.createDocument("", "", null);
- xslDoc.async = false;
- xslDoc.load("guestbook/guestbook.xsl");
- // 定義XSLTProcessor對象
- var xsltProcessor = new XSLTProcessor();
- xsltProcessor.importStylesheet(xslDoc);
- // transformToDocument方式
- 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
- {
- // 支持IE瀏覽器
- 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!");
- }
【編輯推薦】