I'm attempting to use JavaScript to parse a VMAP file. I currently have working code that looks something like this:
function equalsIgnoreCase(a, b) {
return a.toLowerCase() == b.toLowerCase();
}
for(var i = 0, l = xml.childNodes.length; i < l; i++) {
var node = xml.childNodes[i];
if( equalsIgnoreCase( node.tagName, 'adbreak' ) ) {
// Do stuff
} else {
// Unexpected tag
}
}
The problem with this approach is that I have a lot more tags that I'm checking for than just AdBreak
(custom extensions) and it's extremely redundant to write massive if/else waterfalls of the form:
if( equalsIgnoreCase( node.tagName, 'adbreak' ) ) {
// ...
} else if( equalsIgnoreCase( node.tagName, 'otherTag1' ) {
// ...
} else if( equalsIgnoreCase( node.tagname, 'otherTag2' ) {
// ...
} else if....
I would ideally like to use XPath to solve all of my problems. Then all of my code could be condensed into one-liners:
var contentURI = XpathWrapper( xml, 'Extensions/MyExtension/Content' ).iterateNext().text();
var splashScreen = XpathWrapper( xml, 'Extensions/MyExtension/Splash' ).iterateNext().text();
// ...
The problem with this is that Xpath is case-sensitive. I found a few suggestions online saying that you can use the transform
method to make Xpath case-insensitive, however this hasn't worked in practice:
// This fails:
XpathWrapper( xml, "*[transform(node(), 'ABCD...', 'abcd...') = 'extensions']" )
I did find one piece of advice that worked, however it's far from useful:
// This works:
XpathWrapper( xml, "Extensions | EXTENSIONS | extensions | EXTensions | ..." );
Is there any way to make this work (without having to type every string 800 ways)? Note that I don't need to maintain the structure of the original XML, so if there's a way to simply convert the entire document to lower-case before performing any Xpath searches, this is acceptable.