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;
Code:
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");
The book on page 540 and 541 shows how to create an xslt dom object using the function:
createThreadSafeDocument()
but it doesn't give an idea on how to create the apropriate code for other browsers. The above is an IE only approach.
I searched on the web but maybe I am using the wrong search criteria
Can you advise?
thanx
Kevin