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.

That third option is what this post is really all about. After doing some research I have found a few lovely developers who created a wrapper for Internet Explorer to help it behave similarly to Mozilla, thus creating nice cross-browser XML parsers. Here are the three bigger ones.

  • Sarissa is my personal favorite of the three. It’s a cross browser library that handles making document objects, xmlHTTPRequest, xPath parsing, XML to XSLT transformations, and converting XML into strings. There is a lot of documentation on it, the forums are fairly active, it’s an active project, AND besides working for Mozilla and IE, it also partically supports Safari and Opera (which have incomplete native functionality).
  • Glazkov’s XPath Parser is by far the easiest of the three to use. It lets you use XPath parsing to traverse through the HTML DOM. I was pretty pleased with it and even managed to make a few wrapper functions for it to make it even easier to use. Unfortuantely, through, it only works with objects that are in your normal document’s DOM – that is, it won’t work on an XML type document. Bummage. It’s also been put in hiatus. Still, if you’re only using it to go through your own DOM, it’s the best one to go with.
  • Google AJAXSLT – I’m really only including this because it’s Google. It does, from what I can tell, AJAX calls, XPath parsing, and XML to XSLT transformations. The project itself isn’t terribly well documented and has been stagnant since October 2005. However people seem to enjoy it and they use something similar to this for Google Homepages, and they’ve said recently that the project isn’t abandoned, just not a priority. So if you can figure out how to use it, it’s probably pretty neat.
You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.

Subscribe to RSS Feed Follow me on Twitter!