将 IMPORTXML Xpath 查询格式化为 Google 表格的可读数据

Formatting IMPORTXML Xpath query into readable data for Google Sheets

我正在将 XML 数据导入具有以下结构的 google sheet: 编辑:URL:https://sonicstate.com/news/tools/revive_stats/client_camp_3785.xml

<data>
    <campaignId>15802</campaignId>
    <campaignName>Some name</campaignName>
      <startDate>
       <year>2021</year>
       <month>12</month>
       <day>02</day>
     </startDate>
     <endDate>
       <year>2021</year>
       <month>12</month>
       <day>13</day>
     </endDate>
</data>
<data>
.... another record
</data>

我希望导入的多行结果与日期值连接在一起,这样它们就可以出现在 sheet对于每条记录。 例如

15802 | Some name | 2021/12/02 | 2021/12/13

15803 | Another name | 2021/11/30 | 2021/12/04

我试过:

IMPORTXML("myurl" , "//data/campaignId | //data/campaignName | //data/startDate/year | //data/startDate/month|//data/startDate/day")

但每个值 returns 位于单独的行中,包含年、月、日的单元格 例如:

15802
Some Name
Year | Month | Day
15802
Another Name
Year | Month | Day
etc

我也试过:


IMPORTXML("myurl" , "concat(//data/campaignId , //data/campaignName , //data/startDate/year,'/', //data/startDate/month,'/',//data/startDate/day")

但那只有 returns 一条记录。我正在努力寻找合适的术语来搜索我想要实现的目标。此外,Sheets XMLIMPORT 使用 XPath 1.0,它限制了可用的功能

试试这个

在 A1 中:您的 url

在 A2 中:您的函数没有任何变化

在 B1 中:输入 5,因为您要求每个项目有 5 个值

在 B2 中:

=ARRAYFORMULA(VLOOKUP(SEQUENCE(ROUNDUP(COUNTA(A2:A)/B1),B1,ROW(A2)),{ROW(A2:A),A2:A},2,0))

https://docs.google.com/spreadsheets/d/1nFcPgXgRc11-WWICG8Y8KEsB4qAoOt1lIbptHzXdC4M/edit?usp=sharing

尝试:

=INDEX({IMPORTXML(A1, "//data/campaignId"),
 IMPORTXML(A1, "//data/campaignName"), 
 IMPORTXML(A1, "//data/startDate/day")&"/"&
 IMPORTXML(A1, "//data/startDate/month")&"/"&
 IMPORTXML(A1, "//data/startDate/year")})

尝试:

=INDEX(SUBSTITUTE(TRIM(SPLIT(FLATTEN(SPLIT(QUERY(FLATTEN(QUERY(TRANSPOSE(
 IFERROR(IF(0=MOD(ROW(A:A)-1, 5)-{0, 1, 4, 3, 2}, {"♦","","","",""}&TEXT(
 IMPORTXML(A1, "//data/campaignId|//data/campaignName|//data/startDate/year|//data/startDate/month|//data/startDate/day"), 
 {"@","@","@","@","@"})&{"♠","♠","","♣","♣"}, ))),,9^9)),,9^9), "♦")), "♠")), "♣ ", "/"))

formula explanation


更短的效果:

=INDEX(SUBSTITUTE(TRIM(SPLIT(FLATTEN(SPLIT(QUERY(IFNA(
 CHOOSE(MATCH(MOD(SEQUENCE(999)-1, 5), {0, 1}), "♦", )&
 IMPORTXML(A1, "//data/campaignId|//data/campaignName|//data/startDate/year|//data/startDate/month|//data/startDate/day")&
 CHOOSE(MATCH(MOD(SEQUENCE(999)-1, 5), {0, 1, 2, 3, 4}), "♠","♠","♣","♣","")),,9^9), "♦")), "♠")), "♣ ", "/"))