functioninitialize(){varxmlDoc;varxslDoc;
//判断浏览器的类型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对象varxsltProcessor=newXSLTProcessor();xsltProcessor.importStylesheet(xslDoc);varoResultFragment=xsltProcessor.transformToFragment(xmlDoc,document);//将解析过的文本输出到页面varoDiv=document.getElementById("guestbookPanel");oDiv.appendChild(oResultFragment);}catch(e){alert("error:003");}}elseif(typeofwindow.ActiveXObject!='undefined'){//varxmlDoc=Server.CreateObject("Msxml2.DOMDocument.4.0");//支持IE浏览器xmlDoc=newActiveXObject('Microsoft.XMLDOM');xslDoc=newActiveXObject('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("Browserunknown!");}}
javascriptdom处理XSL显示数据的第二种方式。
主要代码如下:
varxmlDoc;varxslDoc;
//判断浏览器的类型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对象varxsltProcessor=newXSLTProcessor();xsltProcessor.importStylesheet(xslDoc);//transformToDocument方式varresult=xsltProcessor.transformToDocument(xmlDoc);varxmls=newXMLSerializer();document.getElementById("guestbookPanel").innerHTML=xmls.serializeToString(result);}catch(e){alert("Unabletodoxml/xslprocessing");}}elseif(typeofwindow.ActiveXObject!='undefined'){try{//支持IE浏览器xmlDoc=newActiveXObject('Msxml2.DOMDocument');xslDoc=newActiveXObject('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("Unabletodoxml/xslprocessing");}}else{alert("Browserunknown!");}
新闻热点
疑难解答