2009
04
May
Category: jQuery

If you have come to this post, you are in the same situation I was in. Many Javascript/AJAX framesworks like jQuery do not work with XML namespaces. I came upon an instance at work where I needed to display data from an RSS feed from Zoho Creator. Part of the problem was some of the nodes had default namespaces and others had custom namespaces. I needed to access the data in the custom namespaces and it just wouldn’t work in Firefox or IE. It worked just fine in Safari.

This is a sample output of the RSS feed from Zoho with the data replaced with dots:

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<rss version=”2.0″ xmlns:zc=”http://creator.zoho.com/rss/”>
<channel>
<title>…</title>
<link>…</link>

<item>
<title>…</title>
<link>…link>
<description>…</description>
<guid isPermaLink=”true”>…</guid>
<zc:image>…</zc:image>
<zc:location>…</zc:location>
<zc:startdate>…</zc:startdate>
<zc:enddate>…</zc:enddate>
</item>

</channel>
</rss>

What I was doing with jQuery was looping over the “item” nodes using the jQuery each() method as such:

var items = $(data).find(“item”);
jQuery.each(items, function(i)
{
alert($(this).find(“location”).text())
});

The problem was that this only worked in Safari. In Firefox and IE nothing was returned. Firefox and IE don’t like the custom namespaces. The only way I have found to get this to work is to use this method:

var items = $(data).find(“item”);
jQuery.each(items, function(i)
{
alert($(this).find(“[@nodeName=zc:location]“).text())
});

There are 3 comments so far

1
July 24, 2009 @ 7:28 am
Liza wrote:

Thanks, I needed this exact solution (in Safari, actually since I had multiple nodes with different namespaces and needed to select one and not the other).

2
October 13, 2009 @ 11:53 pm
cravr wrote:

This was a lifesaver! Thank you!

3
November 12, 2009 @ 8:56 pm
vicente wrote:

There’s nothing better than a good and succinct example. Congratulations!
Thanks!

Add a Comment

Allowed tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">