使用javascript在客户端通过 XSLT 将 XML 文档转换为 XHTML,兼容IE和Firefox

<script type="text/javascript">
loadXML = function(fileRoute){
    xmlDoc=null;
     if (window.ActiveXObject){
        xmlDoc = new ActiveXObject('Msxml2.DOMDocument'); xmlDoc.async=false; xmlDoc.load(fileRoute);
    }
    else if (document.implementation && document.implementation.createDocument){
        var xmlhttp = new window.XMLHttpRequest();
        xmlhttp.open("GET",fileRoute,false);
        xmlhttp.send(null);
        xmlDoc = xmlhttp.responseXML; //返回document对象
    }
    else {xmlDoc=null;}
    return xmlDoc;
}
var Browser={};
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

var xmlObj=loadXML("cdcatalog.xml");
var xslObj =loadXML("cdcatalog.xsl");

if(Browser.isMozilla==true){
//firefox没有实现xml.transformNode,添加transformNode方法。
    Node.prototype.transformNode = function (oXslDom) {
        var oProcessor = new XSLTProcessor();
        oProcessor.importStylesheet(oXslDom);
        var oResultDom = oProcessor.transformToDocument(this);
        var xmls = new XMLSerializer();
        return xmls.serializeToString(oResultDom);                
    }
}
document.write(xmlObj.transformNode(xslObj));
</script>

查看文件 cdcatalog.xsl  cdcatalog.xml

你可能感兴趣的:(使用javascript在客户端通过 XSLT 将 XML 文档转换为 XHTML,兼容IE和Firefox)