#155 closed enhancement
Get Namespaced Elements in XML Documents
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | low | Milestone: | 1.5 |
Component: | core | Version: | 1.2.2 |
Keywords: | xml namespaces | Cc: | |
Blocked by: | Blocking: |
Description (last modified by )
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]) : ret[i].getElementsByTagName(tag)); }
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(/[^:]+:/, '').toUpperCase()==m[2].toUpperCase()";
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
Description: | modified (diff) |
---|---|
Priority: | major → minor |
comment:3 Changed 15 years ago by
comment:6 Changed 15 years ago by
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
Milestone: | → 1.2.4 |
---|
comment:8 Changed 14 years ago by
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
Keywords: | xml namespaces added |
---|---|
Milestone: | 1.2.4 → 1.5 |
Owner: | set to [email protected]… |
Priority: | minor → low |
Reporter: | changed from [email protected]… to [email protected]… |
Status: | new → pending |
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
Status: | pending → closed |
---|
Automatically closed due to 14 days of inactivity.
comment:15 Changed 10 years ago by
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.
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 ofc.nodeName.toUpperCase()
1284 : n.nodeName.replace(/^.*:/,'').toUpperCase()
instead ofn.nodeName.toUpperCase()
Hope this will be implemented as it blocks the use of jQuery with namespaced XMLs.
Orange