selectSingleNode and FireFox
Ajax is cool, for sure. However sometimes its not so easy to work with, Ajax brings back a problem to web development that had been going by the wayside for a few years. You see when I started back in early 2000, we had MS IE, Netscape 4, AOL and a slew of other browsers to support. Writing interactive web programs for that collection of browsers was roughly akin to writing source code for multiple platforms. Actually that's just what it is, i guess the analogy works too well. ASP.NET made a lot of this much nicer, ok well in truth I worked on intranet based projects that locked into one browser. So while the problem may not have gotten any better I couldn't see it ;) All that changed a couple years ago, working on very public sites again they had to work in FF and IE, granted this is much better than the old browser collection. So one problem i had yesterday was Firefox kept barking (rudely) about selectSingleNode() is not a function. It sure is, its part of an xml element! i said, but it persisted. After much googling, i was able to write this function to wrap the functionality. I'm sure there are better ways of doing this, but this is the one I found. Judging by the amount of googling that i did to find the bits & parts, there are some out there with a similar problem. I guess what i don't understand is why it takes three lines of code to do this in FF and only one in IE. Is it that coming from an IE background my point of view is skewed? Or is this just legitimately more difficult in FF? I'll bet that most frameworks (atlas/etc..) have this built in and I just didn't see it. Post a comment and tell me if that's it!
function SelectSingleNode(xmlDoc, elementPath)
{
if(window.ActiveXObject)
{
return xmlDoc.selectSingleNode(elementPath);
}
else
{
var xpe = new XPathEvaluator();
var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return results.singleNodeValue;
}
}