Classic Note Entries

Transform xml on client with javascript

Overview


This loads the xml and xslt resources synchronosly (false param on open.). It transforms the xml to html and then inserts it in the example element in the document.

<script>
function loadXMLDoc(resource){
    if (window.XMLHttpRequest){
        xhttp=new XMLHttpRequest();
    }
    else{
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",resource,false);
    xhttp.send("");
    return xhttp.responseXML;
}
function displayResult(){
    xml=loadXMLDoc("cdcatalog.xml");
    xsl=loadXMLDoc("cdcatalog.xsl");
    // code for IE
    if (window.ActiveXObject){
        ex=xml.transformNode(xsl);
        document.getElementById("example").innerHTML=ex;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument){
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        document.getElementById("example").appendChild(resultDocument);
    }
}
</script>