Page 1 of 1

Javascript and XML...how to convert an Xslt file to DOM obj

Posted: Tue Dec 08, 2009 10:10 pm
by krraleigh
Working through the Javascript book "Professional Javascript for Web Developers" by Wrox, author "Zakas".
In particular I am working through his section on how to handle XML processing in Javascript.

The book gives a really good cross browser function for handling xslt transformation, but it stops short in
telling me how to create a dom object out of an xslt file. "myfile.xslt"

I have never worked with any of the XML methods or properties before so I am not really sure of what I am
looking for. I do have a function that creates a dom object from an xml string that is cross browser functional.
Can I use this function to create an xslt dom object; I know enough to know that their is a huge different between
xslt and xml files, but I don't know enough to figure how to convert the xslt file to dom object

Code: Select all

 
function parseXml(xml){
    
    var xmldom = null;
    
    if(typeof DOMParser != "undefined"){
        xmldom = (new DOMParser()).parseFromString(xml, "text/xml");
        var errors = xmldom.getElementsByTagName('parseError');
        if(errors.length){
            throw new Error{"xml parsing error:" + errors[0].textContent);
        }
    } else if (document.implementation.hasFeature("LS", "3.0")){
        var i = document.implementation;
        var parser = i.createLSParser(i.MODE_SYNCHRONOUS, null);
        var input = i.createLSInput();
        input.stringData =xml;
        xmldom = parser.parse(input); // if this fails it automatically throws an error with the data
    }else if(typeof ActiveXObject != "undefined"){
        var xmldom = createDocument();
        xmldom.loadXML(xml);
        if(xmldom.parseError != 0){
            throw new Error("xml parsing error: " + xmldom.parseError.reason);
        }
    } else {
        throw new Error("no parser available");
    }
    return xmldom;
} // end cross browser function for creating DOM object from strings
 
function createDocument(){
    
    if(typeof arguments.calle.activeXString != "string"){
        var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"];
        
        for(var i=0, len=versions.length; i<len; i++){
            try{
                var xmldom = new ActiveXObject(versions[i]);
                arguments.calle.activeXString = versions[i];
                return xmldom;
            } catch (ex) {
                //skip
            }
        }
    }
    return new ActiveXObject(arguments.callee.activeXString);
}
var xmldom = parseXml("my xml string");
 
 
I searched on the web a bit but maybe I am using the wrong search criteria
Can you advise?

thanx
Kevin