Ticket #155 (closed enhancement)
Get Namespaced Elements in XML Documents
| Reported by: | slantedtiles@… | Owned by: | slantedtiles@… |
|---|---|---|---|
| Priority: | low | Milestone: | 1.5 |
| Component: | core | Version: | 1.2.2 |
| Keywords: | xml namespaces | Cc: | |
| Blocking: | Blocked by: |
Description (last modified by flesler) (diff)
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
comment:1 Changed 7 years ago by john
- Priority changed from major to minor
- Description modified (diff)
comment:3 Changed 6 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.
Orange
comment:4 Changed 5 years ago by george.petro
any status on this one? Seems still not implemented...
comment:6 Changed 5 years ago by flesler
- need set to Review
- Description modified (diff)
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:8 Changed 5 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 3 years ago by SlexAxton
- Status changed from new to pending
- Reporter changed from slantedtiles@… to slantedtiles@…
- Priority changed from minor to low
- Owner set to slantedtiles@…
- Version set to 1.2.2
- Milestone changed from 1.2.4 to 1.5
- Keywords xml namespaces added
It's been 2 years and 3 spam messages since anyone checked in on this, anyone still care?
comment:13 Changed 3 years ago by trac-o-bot
- Status changed from pending to closed
Automatically closed due to 14 days of inactivity.
comment:14 Changed 20 months ago by JP
This is still an issue as of 1.4.2, not sure about 1.6.x
comment:15 Changed 7 months ago by jhbrock@…
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
