0

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.

stevendesu
  • 15,753
  • 22
  • 105
  • 182

1 Answers1

1

I don't know much about XPath in JavaScript world, but looking solely at your XPath there are at least 2 things need to be fixed :

  1. Use translate() instead of transform()
  2. Use name() instead of node() to match by element name

Try using this XPath :

//*[translate(name(), 'ABCD...', 'abcd...') = 'extensions']
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • This worked perfectly! Guess I just suck at XPath. Here I thought the browser didn't support XPath filters. Tested in Safari, Firefox, and Chrome. Hopefully it will work in IE... – stevendesu Jul 02 '14 at 13:18