Derek 朕會功夫
Derek 朕會功夫

Reputation: 94359

Select a node with namespace in jQuery

I'm using YQL to fetch data using JSONP, and it returns a string of XML. I parse it using $.parseXML and put it inside the jQuery selector and try to select a node. However, it contains a namespace (for example, yweather:) and jQuery seems to not working as it should be.

From other SO answers they suggested that doing \\: will solve it. It does, but only when the data I received is XML (mine is with JSONP.)

$.ajax({
    url: "http://query.yahooapis.com/v1/public/yql",
    data: {
        q: "select item from weather.forecast where location=48907",
        format: "jsonp"
    },
    dataType: "jsonp"
}).success(function(data){
    var xml = $.parseXML(data.results[0]);
    console.log($("yweather\\:condition", xml));
});

It didn't match anything.

Upvotes: 1

Views: 710

Answers (1)

redV
redV

Reputation: 684

Could not figure out why it is not working, also other answers suggested escaping : with \\. But it is not working. So I have tried in this way and it is working. This is also equal to jQuery's find method and it is working demo

Code is

  var xml = $.parseXML(data.results[0]);
  xml = $(xml).find('channel item');
  var weatherList = xml.find('*').filter(function(){
     return this.tagName === "yweather:condition";
  });
  console.log(weatherList);

Hope this helps.

Upvotes: 1

Related Questions