5

We've got an XML file where some nodes are namespaced. Basically the file looks like this:

<foo>
    <bar xmlns:x="http://www.example.com/">
        <x:bla foo="bar" />
    </bar>
</foo>

What we want to achieve is that we want to select the x:bla node, but unfortunately we don't know in advance the node's name, just its namespace. Hence all we know is basically that it's a x:* node.

Now, the question is: How do we select this node by using jQuery's find method once we have parsed the XML file by using $.parseXML?

Using $(xml).find("x\\:bla, bla") works, but only if I know that the node is called bla.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • You can find a possible solution may be [here](http://stackoverflow.com/questions/2563101/how-to-find-extract-data-from-xml-with-jquery) – VicoMan Nov 13 '12 at 09:23
  • Thanks for the hint, but this is not about namespacing at all (or did I miss something?). – Golo Roden Nov 13 '12 at 09:58
  • 1
    Maybe this will help: [link](http://stackoverflow.com/questions/853740/jquery-xml-parsing-with-namespaces) [1]: http://stackoverflow.com/questions/853740/jquery-xml-parsing-with-namespaces – Oliver Nov 13 '12 at 10:21

4 Answers4

5

You can use the attribute-contains jQuery-selector (see here)

xml.find("[nodeName*='x:']")
  • 3
    No it doesn't. It ignores everything XML's namespaces are ... the creator of the XML fragment might change the local prefix at any time which will break your implementation ... even just upgrading his tools (which might use some kind of serialization behind the scenes) might break your app ... – Ingo Nov 13 '12 at 10:47
  • Hmmmm ... yes, this is basically true, but what other solution is there (at least in the scenario I'm at right now I don't care about the definitely existent, but subtle difference between the namespace and the local prefix being used for that namespace, hence it's okay)? – Golo Roden Nov 13 '12 at 11:52
1

.parseXML() is giving me XML parse error, it seems it can't handle the <x:bla.. syntax.

So I just used jQuery with a custom selector to find elements by namespace:

$.expr[':'].findNS = function(obj, idx, meta, stack) {
    if (obj.nodeName.indexOf(meta[3].toUpperCase()+':') === 0) {
        return true;
    }
    return false;
};

var xml="<foo><bar><x:bla atr='attvalue' /></bar></foo>";

alert( $(xml).find(':findNS(x)').attr('atr') ); // Alerts 'attvalue'

See working demo

Nelson
  • 49,283
  • 8
  • 68
  • 81
1

jQuery XML parsing with namespaces shows how to search for a literal namespace prefix. I would consider this an "evil" workaround, because it's no real XML processing. But it looks like jQuery does not support any real namespace handling. So you probably have to go for that solution.

Community
  • 1
  • 1
Achim
  • 15,415
  • 15
  • 80
  • 144
0

I had a similar need, and wanted to handle namespaces properly. Here is an example that worked for me. In this case, I am reading an RSS feed that contains MediaRSS properties.

var xmlDoc = $.parseXML(data);
var $xml = $(xmlDoc);
var ns = {};
$xml.find('item').each(function() {
    console.log('title = ' + $(this).find('title').text());
    console.log('link  = ' + $(this).find('link').text());
    console.log('thumb = ' + 
        $(this.getElementsByTagNameNS('http://search.yahoo.com/mrss','thumbnail')).attr('src'));
});
Eric Lange
  • 1,755
  • 2
  • 19
  • 25