简体   繁体   中英

Google Sheets ImportXML XPath - sum vs avg

Gurus of GoogleSheets/ImportXML/XPath coding,

Quick question... two ImportXML statements with the only difference being the XPath Aggregate function: sum works, avg doesn't.

=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)")

NOTE: For the page in the formula, there goal is to find the row with the highest @price... then get the quantity/volremaining and price. However, when there are multiple rows at the highest price, I need to sum(@volremaining) and avg(@price) since I only want one row returned. I can use the [1] offset to get this value...

=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")

... but the exercise to get min()/max()/avg() to work is failing.

Can't for the life of me figure out the difference, other than the W3C XPath Site shows fn:avg with double brackets ((arg1,arg2,arg3)), while fn:sum is single brackets (arg1,arg2,arg3).

Your assistance will be greatly appreciated.

Regards,

GA

Google Sheet's IMPORTXML() function is exceptionally buggy, but in this case the reason is different.

IMPORTXML() only supports XPath 1.0 functions, but not XPath 2.0 ones. sum() is an XPath 1.0 function, whereas avg() , min() and max() are XPath 2.0 functions that you cannot use in Google Sheets.

See the relevant part of the XPath 1.0 specification - as you can see, avg() is not listed as a number function. It is only listed as a member of the XPath 2.0 and XQuery aggregate functions . By the way, w3schools has nothing to do with W3C - and you should never ever trust content from w3schools.

To sum this up, avg() is not available in an XPath expression - just retrieve all values and use a Google Sheets function instead to calculate an average .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM