【问题标题】:Google Sheets ImportXML XPath - sum vs avg谷歌表格 ImportXML XPath - sum vs avg
【发布时间】:2015-05-29 15:01:30
【问题描述】:

GoogleSheets/ImportXML/XPath 编码专家,

快速提问...两个 ImportXML 语句,唯一的区别是 XPath Aggregate 函数:sum 有效,avg 无效。

=ImportXML("http://api.eve-marketdata.com/api/item_orders2.xml?char_name=Demo&buysell=b&type_ids=17464&station_ids=60003760","sum(/emd/result/rowset/row[not(preceding-sibling::row/@price > @price or following-sibling::row/@price > @price)]/@volremaining)")

=ImportXML("http://api.eve-marketdata.com/api/item_orders2.xml?char_name=Demo&buysell=b&type_ids=17464&station_ids=60003760","avg(/emd/result/rowset/row[not(preceding-sibling::row/@price > @price or following-sibling::row/@price > @price)]/@volremaining)")

注意:对于公式中的页面,目标是找到@price 最高的行...然后获取数量/剩余数量和价格。但是,当有多行价格最高时,我需要 sum(@volremaining) 和 avg(@price) 因为我只希望返回一行。我可以使用 [1] 偏移量来获取这个值...

=ImportXML("http://api.eve-marketdata.com/api/item_orders2.xml?char_name=Demo&buysell=b&type_ids=17464&station_ids=60003760","/emd/result/rowset/row[not(preceding-sibling::row/@price > @price or following-sibling::row/@price > @price)][1]/@volremaining")

...但是让 min()/max()/avg() 工作的练习失败了。

除了W3C XPath Site 显示 fn:avg 和双括号 ((arg1,arg2,arg3)) 之外,我一辈子都无法弄清楚其中的区别,而 fn:sum 是单括号 (arg1,arg2 ,arg3)。

我们将不胜感激。

问候,

GA

【问题讨论】:

    标签: xml xpath xml-parsing google-sheets


    【解决方案1】:

    Google Sheet 的 IMPORTXML() 函数异常错误,但在这种情况下,原因不同。

    IMPORTXML() 仅支持 XPath 1.0 函数,不支持 XPath 2.0 函数。 sum() 是 XPath 1.0 函数,而 avg()min()max() 是无法在 Google 表格中使用的 XPath 2.0 函数。

    查看relevant part of the XPath 1.0 specification - 如您所见,avg() 未列为数字函数。它仅被列为XPath 2.0 and XQuery aggregate functions 的成员。顺便说一句,w3schools 与 W3C 无关 - 您永远不应该相信来自 w3schools 的内容。

    总而言之,avg() 在 XPath 表达式中不可用 - 只需检索所有值并使用 Google Sheets function instead to calculate an average

    【讨论】:

    • 感谢您的快速回复,Mathias。我不知道 XPath 1.0 的限制……我希望 Google 将来会将 Sheets 扩展到 2.0 或 3。既然我知道了,我将阅读更多关于此的内容。非常感谢,最诚挚的问候,GA
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多