You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sheets IMPORTXML空值处理:获取天气数据coverage属性及空值判断失败

解决DWML天气数据提取问题

问题分析

你需要从指定的DWML天气数据接口中,按小时提取weather-conditions节点的两种状态:

  • 若节点包含降雨类型的value,提取其coverage属性值
  • 若节点标记为xsi:nil="true"(代表无概率),返回标识文本

之前用|合并两个XPath的写法会导致结果顺序错乱,无法对应到每个小时的节点,因此需要针对每个weather-conditions节点单独处理。

解决方案

使用XPath 1.0的条件拼接写法,确保每个weather-conditions节点返回对应结果,适配Google Sheets的IMPORTXML函数:

最终公式

=IMPORTXML("https://forecast.weather.gov/MapClick.php?lat=33.4456&lon=-112.0674&FcstType=digitalDWML", "//weather-conditions/concat(substring('无概率',1,3*number(@xsi:nil='true')), substring(value[contains(@weather-type,'rain')]/@coverage,1,100*number(not(@xsi:nil='true'))))")

公式说明

  • //weather-conditions:遍历所有小时级的天气条件节点
  • concat(...):拼接两种状态的结果
    • 当节点为xsi:nil="true"时,number(@xsi:nil='true')返回1,提取"无概率"完整文本
    • 当节点不为空时,number(not(@xsi:nil='true'))返回1,提取降雨类型value的coverage属性值(100为足够长的长度,确保完整提取)

命名空间兼容写法

如果@xsi:nil因命名空间问题无法识别,可替换为以下写法:

=IMPORTXML("https://forecast.weather.gov/MapClick.php?lat=33.4456&lon=-112.0674&FcstType=digitalDWML", "//weather-conditions/concat(substring('无概率',1,3*number(@*[local-name()='nil' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']='true')), substring(value[contains(@weather-type,'rain')]/@coverage,1,100*number(not(@*[local-name()='nil' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']='true'))))")

内容的提问来源于stack exchange,提问作者Will McArthur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:24:19