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.

5
  • 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 and select json format and note the json format added to url generated in console Commented Nov 25, 2013 at 4:14
  • demo for same location in michigan , data all in json jsfiddle.net/faNaM Commented Nov 25, 2013 at 4:24
  • @charlietfl - I just noticed YQL allows cross origin requests, thanks. Commented Nov 25, 2013 at 4: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 Commented Nov 25, 2013 at 4:36
  • @Derek朕會功夫 Please have a look at my answer. Commented Nov 25, 2013 at 5:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
To be fair it did solve the problem. Thanks! However, the reason why jQuery can't select it remains unknown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.