【问题标题】:Selecting a particular field from Google's "unofficial" weather API using YQL使用 YQL 从 Google 的“非官方”天气 API 中选择特定字段
【发布时间】:2012-02-27 11:59:16
【问题描述】:

According to another question我可以使用 YQL 查询选择单个 XML 字段,并且在我测试时它工作得很好:

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

但是,当我尝试从 Google's "unofficial" weather XML API 中选择单个 XML 字段时,我总是得到空结果(尽管 SELECT * 确实有效)。

我知道我对 XML 和 YQL 的理解都不完整,但我缺少什么? (Here's my query in the YQL console)

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

我的预期查询

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

结果

<?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>

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>

【问题讨论】:

    标签: xml select yql google-weather-api


    【解决方案1】:

    事实证明,Google 天气 API 并没有什么特别之处,而是关于 SELECT 参数如何与 YQL 查询中的 itemPath 参数交互。

    我个人觉得 YQL 文档对于我们这些对它所依赖的一些技术只有初步了解的人来说很难使用。

    当您SELECT ... FROM rss 时,似乎有一个隐含的itemPath 指向提要的重复item 字段。但是当您SELECT ... FROM xml 时,您需要手动指定itemPath。如果您的 XML 中没有重复的部分,那么您将其设置为什么是非常随意的。

    现在,一旦您设置了itemPath,您就可以使用点符号来SELECT 特定字段。请注意,您可以使用SELECT 点表示法指定属于 XML 属性的子字段,但不能使用 itemPath 中的斜杠表示法指定它们,但没有诊断可以帮助您找到此类初学者错误...

    我的新查询

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

    结果

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2017-04-06
      相关资源
      最近更新 更多