Sunday, 8 March 2009

JQuery, XML and namespaces

Being awake at the early hours of the morning I decided to try and take my mind off of product strategy and increasing market share, so I decided to play around with Altio 5.3 (pre beta) REST services.

The challenge I set myself was to use JQuery to retrieve data through the AltioLive Presentation Server (APS). I thought it would be easy, after all it’s easy in AltioLive.

The problem I encountered was namespaces.

It seems JQuery doesn’t handle namespaces very well, as described in ticket 155.

The solution for me was to use the following syntax ‘namespace\\:element_name’ ;

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
...
<atom:content type="text">blah blah</atom:content>
</atom:entry>

Now if that document is stored in a variable named xml:

$(xml).find('atom\\:content').eq(0).text()

Will return the text of the atom:content element.

 

I found this out courtesy of www.xml.com http://www.xml.com/cs/user/view/cs_msg/4298.

 

Not sure if the syntax is correct, but it works in FF and IE using JQuery 1.3.2. I managed to read a fair number of blog entries, and information on processing XML with namespaces, the above was the only approach that worked for me.

Other sources of info:

http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html

4 comments:

Unknown said...

Happy I stumbled upon your article. I've had to use so many band-aides for this problem. going to save me a lot of time.

Dustin Ted Whitney said...

Hey, this helped me a lot too. Thanks!

Gary Thompson said...

Stu, Dustin - I'm glad the entry is of help.

hajn said...

just stumbled upon this nearly 2 year old post and wanted to thank you, you really helped me!