Friday, April 27, 2007

Never Mind the Namespaces: An E4X client

Bob DuCharme published "Never Mind the Namespaces: An XSLT RSS Client" way back in January 2003. It is one of my favourite articles and I have regularly returned to it over the years.

Essentially it demonstrates how the XPath local-name() function can be used to create a single universal XSLT stylesheet that would work with the many RSS formats that share a similar construction but different namespace. As far as the typical XML processor is concerned a difference in namespace is usually an extremely significant difference and using local-name() function essentially gives you the power to write XSLT thats says "I do not care what namespace the 'item' element is in just give it to me".

I have started using E4X in real projects recently and it also supports functionality equivalent to the XPath local-name() function. Ignoring how data is loaded into the DOM, the following are pretty much equivalents.





This ability means we can write an E4X script that can also render RSS/Atom paying little heed to namespaces.

The following example is probably not perfect but in theoretically it could load any RSS/Atom and render it. Additional action would be necessary to bypass XMLHttpRequest security constraints for accessing remote feeds.

Assuming you have a decent browser that supports E4X the following should work (e.g. Firefox 2).