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.
What I am trying to ascertain is:
His section on dealing with XPath notes that there are some browsers that won't work with the XPathEvaluator.
He states that I should use the "corresponding methods on the Document" to make up for the short coming in
some browsers. Could someone give me an idea on what he means by "corresponding methods on the Document"?
His crossbrowser functions for Xpath:
Code:
// { prefix1: 'uri1', prefix2: 'uri2' } //example of namespace object
}
function selectSingleNode(context, expression, namespaces){
var doc = (context.nodeType != 9? context.ownerDocument : context);
if (typeof doc.evaluate != "undefined"){
var nsresolver = null;
if(namespaces instanceof Object){
nsresolver = function(prefix){return namespaces[prefix];};
}
var result = doc.evaluate(expression, context, nsresolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return (result !== null ? result.singleNodeValue : null);
}else if(typeof context.selectSingleNode != "undefined"){
//create namespace string
if(namespaces instanceof Object){
var ns = "";
for(var prefix in namespaces){
if(namespaces.hasOwnProperty(prefix){
ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' ";
}
}
doc.setProperty("SelectionNamespaces", ns);
}
return context.selectSingleNode(expression);
}else {
throw new Error("no xpath engine found"):
}
}// end function selectSingleNode()
var result = selectSingleNode(xmldom.documentElement, "wrox:book/wrox:author", {wrox: "http://www.wrox.com/" });
// result is an object
//using the same namespaces literal above...
function selectNodes(context, expression, namespaces){ //selecting multiple nodes in a crossbrowser fashion
var doc = (context.nodeType != 9? context.ownerDocument : context);
if (typeof doc.evaluate != "undefined"){
var nsresolver = null;
if(namespaces instanceof Object){
nsresolver = function(prefix){return namespaces[prefix];};
}
var result = doc.evaluate(expression, context, nsresolver, XPathResult.ORDERED_SNAPSHOT_TYPE, null);
var nodes = new Array();
if(result !== null){
for (var i=0, len=result.snapshotLength; i < len; i++){
nodes.push(result.snapshotItem[i];
}
}
return nodes;
} else if (typeof context.slectNodes != "undefined"){
//create namespace string
if(namespaces instanceof Object){
var ns = "";
for(var prefix in namespaces){
if(namespaces.hasOwnProperty(prefix){
ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' ";
}
}
doc.setProperty("SelectionNamespaces", ns);
}
var result = context.selectNodes(expression);
var nodes = new Array();
for (var i = 0, len= result.lenght; i<len; i++){
nodes.push(result[i]);
}
return nodes;
} else {
throw new Error("no xpath engine");
}
}// end function selectNodes();
//function call
var result = selectNodes(xmldom.documentElement, "wrox:book/wrox:author", { wrox: "http://www.wrox.com/" });
thanx kevin