1

I've been working on a big jQuery project for a while and everything was going smoothly until I tried parsing XML with namespaces. I'm using jQuery 1.4.2 and I've tried a few different solutions:

$(xml).find('[nodeName=ns:blah]')...
$(xml).find('ns\\:blah')...

And I tried the jQuery.xmlns.js plugin but it doesn't seem to be working either.

The XML is formatted similar to:

<response xmlns:ns='http://example.com/ex/'>
  <response>SUCCESS</response>
  <action>QUERY</action>
  <data>
    <ns:blah>Trying to reach me!</ns:blah>
  </data>
</response>

Does anyone know of a solution, preferably using jQuery or a cross-browser Javascript implementation?

EDIT: Whoops, I was escaping the correctly in the code, just not in here. I've corrected it.

Community
  • 1
  • 1
ravun
  • 1,523
  • 9
  • 27
  • 45

1 Answers1

1

Your escaping should look like this (backslashes):

$(xml).find('ns\\:blah')...

Example: http://jsfiddle.net/cUhZH/

Upgrade to the latest version of jQuery. I believe there some fixes in 1.4.3 with regard to selecting namespaced attributes.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • It works in your example, but not in my code. I tried copying and pasting my code into jsFiddle and it works fine. – ravun Nov 18 '10 at 19:24
  • Weird, it works if I build a test page and include jquery 1.4.2 but when I'm using the bundled jquery + plugins lib that the other developers are using then it doesn't work. I suppose I need to figure out whether they're really using 1.4.2 like it says. – ravun Nov 18 '10 at 19:44
  • @ravun - You want to be sure to have only *one* version of jQuery loaded. To test which version you're running, try `alert(jQuery.fn.jquery);` and see what it tells you. – user113716 Nov 18 '10 at 19:48
  • It shows 1.4.2. For some reason if I replace the jquery core code with 1.4.4 it works fine. I could see if the others are willing to upgrade. However, another issue is that 'ns\\:blah' doesn't work on all browsers (it didn't work in IE7) and '[nodeName=ns:blah]' doesn't work either. – ravun Nov 18 '10 at 20:15
  • @ravun - Yes, as I originally indicated in my answer, there were some fixes after `1.4.2` with regard to the `:` character in selectors. (I crossed it out because I couldn't reproduce the issue.) With regard to IE7, did it work for you with `1.4.4`? If not, I think there need to be extra considerations made for IE with regard to XML. jQuery doesn't really parse the XML. I believe it just attempts to use the browser's `innerHTML`, and therefore treats it as DOM nodes. I think IE has issues with this because of the custom tag names. – user113716 Nov 20 '10 at 15:39
  • @patrick dw - Thanks. I don't really remember, but I believe it did NOT work in IE with 1.4.4. I ended up using Sarissa.js and it works great. – ravun Nov 29 '10 at 18:44