1

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.

Community
  • 1
  • 1
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • can get data from same table in YQL as json results instead of xml...personally that's easier to work with IMO. Go to [YQL console](http://developer.yahoo.com/yql/console/#h=select+*+from+weather.forecast+where+location%3D48907) and select `json` format and note the json format added to url generated in console – charlietfl Nov 25 '13 at 04:14
  • demo for same location in michigan , data all in json http://jsfiddle.net/faNaM/ – charlietfl Nov 25 '13 at 04:24
  • @charlietfl - I just noticed YQL allows cross origin requests, thanks. – Derek 朕會功夫 Nov 25 '13 at 04:34
  • figured you would notice that by me using `get`. Yes is very under used service, but can be really handy in app with no server support. Have even used it to scrape html using their xpath selectors – charlietfl Nov 25 '13 at 04:36
  • @Derek朕會功夫 Please have a look at my answer. – redV Nov 25 '13 at 05:15

1 Answers1

1

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.

redV
  • 684
  • 1
  • 9
  • 26
  • @Derek朕會功夫 There is no difference between `$('yweather:condition', xml)` in your code and `$(xml).find('*').filter(function(){..})` . Both does the same number of iterations. I assume this is matching your criteria. – redV Nov 25 '13 at 05:35
  • To be fair it did solve the problem. Thanks! However, the reason why jQuery can't select it remains unknown. – Derek 朕會功夫 Nov 25 '13 at 05:42