Skip to content


jQuery .find() on a namespaced XML node fails on Google Chrome

Ran into a problem with a jQuery AJAX script that parses returned XML. It wouldn’t work on Google Chrome when looking for a namespaced xml node using jQuery.find().

Apparently this (xml namespaces) can cause jQuery some minor heartburn but it’s not a show stopper.

A namespaced nodename in XML has a name space prefix a colon then the node name. e.g.

<ns1:ld_part>65989</ns1:ld_part>

Basically namespace prefixes in XML stop collisions if two different coders want to refer to some thing with the same name

<!-- clash -->
<part>Us</part>
<part>Them</part>
<!-- happy co-existence  (same same but different!) -->
<us:part>Us</us:part>
<them:part>Them</them:part>

This Fails on Google Chrome:

 
// resp is XML passed from an ajax call to a server
success: function(resp){
	var jData = $( resp ); // jQueryise it
        var recordTop = jData.find("ns1\\:ld_det"); // search for the node
        // alert( recordTop.size()) returns 0 on Google Chrome

Success:

 
success: function(resp){
var jData = $( resp );
var recordTop = jData.find("[nodeName=ns1:ld_det]"); // this works on google chrome
// alert(recordTop.size()); returns the number of xml nodes boo yah!

This jQuery forum post provided the answer http://forum.jquery.com/topic/jquery-namespaces-xml-parsing-12-1-2010

Versions:

  • Google Chrome version 5.0.375.126 (Fedora 13)
  • jQuery JavaScript Library v1.4.2

Posted in IT Tips.


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Soleil Golden says

    You, sir, are my hero. I was struggling with this for hours! This solved my plug-in’s main bug. I love you. :D

  2. admin says

    LOL. Comment of the month!

    Glad it helped you Soleil.

  3. John Smith says

    Thanks a lot – it saved me hours!



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin