Latest: AjaMyXSL.js source listing /*** ** AjaMyAjax --> XSLT functions library ** use with the core AJAX lib routines in ajaMyCore.js. ** ** This software provided "AS IS," but is FREE to use and share. ** For details see the as-is doc and Creative Commons license: ** http://creativecommons.org/licenses/by-sa/3.0/ ** ** See doc.txt or examples.html for syntax and more info at ** http://AjaMyAjax.com, (c) AjaMyAjax.com */ pbXSLLibReady = true; // notifies core that lib is available var pbXSLLibHeader = "[AjaMyAjax XSL Lib]"; this.processXSL = function() { var xmlDoc = (arguments[0]) ? arguments[0] : null; var xslurl = (arguments[1]) ? arguments[1] : null; var strcontainer = (arguments[2]) ? arguments[2] : null; var stroutputFormat = (arguments[3]) ? arguments[3] : null; var xslDoc = null; if (navigator.userAgent.indexOf("Chrome") != -1 || navigator.userAgent.indexOf("Safari") != -1) { // developer note: php xsl translation works just fine for Chrome and Safari... // you can view a php example at http://ajamyajax.com/phpxsl.html // or download more complete code examples here: // http://ajamyajax.com -or- http://ajamyajax.com/basiccurl.html alert("Not compatible with this browser"); return; } if (!xmlDoc || !strcontainer || !xslurl) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbXSLLibHeader, "XSLT error, check parameters"); } return; } // use a separate xsl file if one in parameter list if (stroutputFormat) { if (stroutputFormat.indexOf(".XSL") == -1) { xslurl = stroutputFormat + ".XSL"; } else { xslurl = stroutputFormat; } } else { xslurl = xslurl.replace(".xml", ".xsl"); } if (window.XMLHttpRequest && document.implementation && document.implementation.createDocument) { // Firefox, Opera... xslDoc = document.implementation.createDocument("", "", null); if (xslDoc) { xslDoc.async = false; if (!loadXSLfile(xslurl, xslDoc)) { xslDoc = null; return; } try { var xslProcess = new XSLTProcessor(); xslProcess.importStylesheet(xslDoc); var xslNode = xslProcess.transformToFragment(xmlDoc, document); } catch (e) { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbXSLLibHeader, "XSLT processing error: " + xslurl); } return; } aja_empty(strcontainer); if (xslNode) { if (navigator.userAgent.indexOf("Firefox") != -1) { // fixes most Firefox disable-output-escaping problems: document.getElementById(strcontainer).style.visibility = "hidden"; document.getElementById(strcontainer).appendChild(xslNode); var nodetext = document.getElementById(strcontainer).innerHTML; if (nodetext.indexOf("XML Parsing Error:") == -1) { nodetext = nodetext.replace(/&/gi, "&"); nodetext = nodetext.replace(/</gi, "<"); nodetext = nodetext.replace(/>/gi, ">"); aja_fill(strcontainer, nodetext); } else { if (pbshowAlertMsgs) { // note, try/catch needed throw new aja_alertMsg(pbXSLLibHeader, "Error parsing document"); } } document.getElementById(strcontainer).style.visibility = "visible"; nodetext = null; } else { document.getElementById(strcontainer).appendChild(xslNode); } } xslProcess = null; xslDoc = null; xslNode = null; } // clean up after: try { if (xslurl) xslurl = null; if (strcontainer) strcontainer = null; if (stroutputFormat) stroutputFormat = null; } finally { xmlDoc = null; } } else { // MS Internet Explorer xslDoc = new ActiveXObject("Microsoft.XMLDOM"); if (xslDoc) { xslDoc.async = false; if (!loadXSLfile(xslurl, xslDoc)) { xslDoc = null; return; } try { var xslNode = xmlDoc.transformNode(xslDoc); if (xslNode) { aja_fill(strcontainer, xslNode); xslNode = null; } else { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbXSLLibHeader, "XSLT processing error: " + xslurl); } return; } } finally { xslDoc = null; } } } // load file or return error function loadXSLfile(filename, xslDoc) { var loadOk = false, retry = 0; try { while (!loadOk && retry < 5) { loadOk = xslDoc.load(filename); retry++; } } catch (e) { loadOk = false; // try-catch is for older Opera... } if (loadOk) { return true; } else { aja_empty(strcontainer); if (pbshowAlertMsgs) { aja_alertMsg(pbXSLLibHeader, "XSLT transformation not complete: " + filename); } return false; } } }