JavaScript XPath Support

At work some of my tasks have led me to fooling around with new interface ideas. I figured this is a good way to get up to scratch with AJAX and related ideas.

Now if you’re pulling in XML with AJAX you know that it can be a perfect pain to parse the file. You’ve got to make sure that the file is converted into a document element (and the methods for doing so are unfortunately not cross-browser), and even afterwards there’s no cross-browser method to finding the tags you want - at least not one that will give you clean code. Basically, your choices are:

  • getElementsByTagName() - it works, but the function is long, and you’ll have more loops and nests than you can shake a stick at. Also, you’re limited to tags.
  • Functions such as firstChild and childNodes - which again, leads to long code and lots of nesting. Plus, using these you don’t even know what the tags are you’re messing with, and heaven forbid you change the XML schema a bit, if you didn’t well-document your javascript it could take you a bit to find the lines you need to alter.
  • XPath and XSLT transformations - which are sadly not cross-browser and in some cases don’t even exist in certain browsers.

Read the rest of this entry »

Comments