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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

It works in your example, but not in my code. I tried copying and pasting my code into jsFiddle and it works fine.
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 - 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.
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 - 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.