0

According to another question I can select individual XML fields with a YQL query and it works just fine when I test it:

SELECT statistics.subscriberCount FROM xml
WHERE url='http://gdata.youtube.com/feeds/api/users/{$id}'

However when I try to select individual XML fields from Google's "unofficial" weather XML API I always get empty results (though SELECT * does work).

I know my understanding of both XML and YQL are not complete but what am I missing? (Here's my query in the YQL console)

(I have managed to query it using XPATH with the itemPath parameter and SELECT *)

My intended query

SELECT current_conditions FROM xml
WHERE url="http://www.google.com/ig/api?weather=Tbilisi"

Results

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="0" yahoo:created="2012-02-27T11:56:15Z" yahoo:lang="en-US">
    <results/>
</query>

Results of SELECT *

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2012-02-27T11:57:18Z" yahoo:lang="en-US">
    <results>
        <xml_api_reply version="1">
            <weather mobile_row="0" mobile_zipped="1" module_id="0"
                row="0" section="0" tab_id="0">
                <forecast_information>
                    <city data="Tbilisi, Tbilisi"/>
                    <postal_code data="Tbilisi"/>
                    <latitude_e6 data=""/>
                    <longitude_e6 data=""/>
                    <forecast_date data="2012-02-27"/>
                    <current_date_time data="1970-01-01 00:00:00 +0000"/>
                    <unit_system data="US"/>
                </forecast_information>
                <current_conditions>
                    <condition data="Overcast"/>
                    <temp_f data="34"/>
                    <temp_c data="1"/>
                    <humidity data="Humidity: 80%"/>
                    <icon data="/ig/images/weather/cloudy.gif"/>
                    <wind_condition data="Wind: NE at 6 mph"/>
                </current_conditions>
                <forecast_conditions>
                    <day_of_week data="Mon"/>
                    <low data="28"/>
                    <high data="37"/>
                    <icon data="/ig/images/weather/mostly_sunny.gif"/>
                    <condition data="Mostly Sunny"/>
                </forecast_conditions>
                <forecast_conditions>
                    <day_of_week data="Tue"/>
                    <low data="30"/>
                    <high data="41"/>
                    <icon data="/ig/images/weather/mostly_sunny.gif"/>
                    <condition data="Mostly Sunny"/>
                </forecast_conditions>
                <forecast_conditions>
                    <day_of_week data="Wed"/>
                    <low data="30"/>
                    <high data="43"/>
                    <icon data="/ig/images/weather/mostly_sunny.gif"/>
                    <condition data="Mostly Sunny"/>
                </forecast_conditions>
                <forecast_conditions>
                    <day_of_week data="Thu"/>
                    <low data="27"/>
                    <high data="43"/>
                    <icon data="/ig/images/weather/mostly_sunny.gif"/>
                    <condition data="Partly Sunny"/>
                </forecast_conditions>
            </weather>
        </xml_api_reply>
    </results>
</query>
Community
  • 1
  • 1
hippietrail
  • 15,848
  • 18
  • 99
  • 158

1 Answers1

0

It turns out not to be anything special about the Google weather API but about how the SELECT parameter interacts with the itemPath parameter in a YQL query.

Personally I find the YQL documentation quite difficult to use for those of us with only a rudimentary understanding of some of the technologies on which it relies.

It seems that when you SELECT ... FROM rss that there is an implied itemPath which points to the repeated item field of a feed. But when you SELECT ... FROM xml you need to specify an itemPath manually. If there's no repeating part of your XML it's pretty arbitrary what you set it to.

Now once you have an itemPath set you can then SELECT particular fields using the dot notation. Note that you can specify subfields which are XML attributes using the SELECT dot notation, but you cannot specify them using the slash notation in the itemPath but no diagnostics help you find such a beginner error...

My new query

SELECT current_conditions.temp_c.data FROM xml
WHERE url="http://www.google.com/ig/api?weather=Tbilisi"
AND itemPath="//weather"

Results

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2012-03-01T10:22:41Z" yahoo:lang="en-US">
    <results>
        <weather>
            <current_conditions>
                <temp_c data="3"/>
            </current_conditions>
        </weather>
    </results>
</query>
hippietrail
  • 15,848
  • 18
  • 99
  • 158