Bug Tracker

Opened 16 years ago

Closed 12 years ago

Last modified 10 years ago

#155 closed enhancement

Get Namespaced Elements in XML Documents

Reported by: [email protected] Owned by: [email protected]
Priority: low Milestone: 1.5
Component: core Version: 1.2.2
Keywords: xml namespaces Cc:
Blocked by: Blocking:

Description (last modified by flesler)

I've recently had the need to improve jQuery's selector support for XML nodes with prefixes. In theory, it should be possible for a node with the node name "D:response" to be selected using just "response", in accordance with getElementsByTagName's rules. There's two issues that prevent that, which I've included fixes for.

The first is that IE supports getElementsByTagName incorrectly, and treats prefixes as part of the tag name. Using rev 234, lines 1228-1233 could be replaced with this:

for ( var i = 0; i < ret.length; i++ ) {
  // create an array of tagnames to search for
  var tags = {};

  // fix for IE leaving in node prefixes
  if (jQuery.browser.msie && ret[i].xml !== undefined && m[2] != '*')
    for (var regex = new RegExp('</?((?:[^:]+:)?' + m[2] + ')\b', 'g'), tag;
      (tag = regex.exec(ret[i].xml)) != null; tags[tag[1]] = true);
  else tags[m[2]] = true;

  for ( var tag in tags )
    r = jQuery.merge( r, tag == "*" ?
      jQuery.getAll(ret[i]) :

If it detects that we're searching an XML document and IE is being used, it'll find all node names with the correct local name, get their complete node names with prefixes, and search for each of those in turn. If IE is not being used or we're searching an HTML document, it'll search only for the specified tag name, so the overhead seems rather minimal.

The second issue is that jQuery prevents nodes with prefixes from being preserved in the filter function, specifically because of line 1014:

"": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",

Which could be replaced with:

"": "m[2]== '*'||a.nodeName.replace(/[^:]+:/,

That'll make sure only the local name is being compared instead of the complete node name. (The best solution would be to use a.localName, but IE doesn't support it, of course...)

If you could look this over and send a reply, I'd be very grateful.

— Mitchell Lane

PS: The next step to this would be adding support for a namespace resolver, perhaps as a third parameter of $() and by using the CSS syntax ("D|response"). Would you have any interest if I were to try and write support for that?

Change History (11)

comment:1 Changed 16 years ago by john

Description: modified (diff)
Priority: majorminor

comment:3 Changed 15 years ago by Orange

Is there any improvement on that point planned ? In jQuery-1.2.1.js, it's only two changes to add on lines :

1260 : c.nodeName.replace(/^.*:/,'').toUpperCase() instead of c.nodeName.toUpperCase() 1284 : n.nodeName.replace(/^.*:/,'').toUpperCase() instead of n.nodeName.toUpperCase()

Hope this will be implemented as it blocks the use of jQuery with namespaced XMLs.


comment:4 Changed 15 years ago by george.petro

any status on this one? Seems still not implemented...

comment:5 Changed 15 years ago by mihxil

Can I vote for this bug to be fixed?

comment:6 Changed 15 years ago by flesler

Description: modified (diff)
need: Review

What about supporting $('b\:foo') like with attribute selectors ?

This could just handled with a plugin with something like:

$.nsTag = function( name ){

return $(document.getElementsByTagName(name));


$.nsTag('b:foo')... (any jQuery method)

comment:7 Changed 15 years ago by flesler

Milestone: 1.2.4

comment:8 Changed 14 years ago by timothy.davi

This seems to not have made it into 1.2.4 http://dev.jquery.com/report/27. Can you update when this will be fixed? $(xml).find('ns:elementName').text() would be nice

comment:12 Changed 12 years ago by SlexAxton

Keywords: xml namespaces added
Owner: set to [email protected]
Priority: minorlow
Reporter: changed from [email protected] to [email protected]
Status: newpending
Version: 1.2.2

It's been 2 years and 3 spam messages since anyone checked in on this, anyone still care?

comment:13 Changed 12 years ago by trac-o-bot

Status: pendingclosed

Automatically closed due to 14 days of inactivity.

comment:14 Changed 11 years ago by JP

This is still an issue as of 1.4.2, not sure about 1.6.x

comment:15 Changed 10 years ago by [email protected]

This is still broken as of 1.8.2. There's mutually incompatible behavior between browsers:

In Firefox, selectors must include the namespace and escape the colon with two slashes, e.g., you would select <ns:foo> with $(data).find("ns
:foo"). However, this doesn't work in Chrome.

In Chrome, selectors must have the namespace dropped, e.g., you would select <ns:foo> with $(data).find("foo"). However, this doesn't work in Firefox.

Note: See TracTickets for help on using tickets.