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

Power Automate中XPath统计分隔符后非空值的表达式调试求助

问题:Power Automate中XPath统计分组数据失败

我在Power Automate流中尝试用XPath表达式实现以下统计需求,但未成功:
按每个条目里|前的子串(如“Data and Analytics”)、|与第一个;之间的子串(如“2024-09”)分组,统计第二个;之后存在非空值的条目数量。

尝试的表达式

xpath(
    outputs('Compose_3'),
    concat(
        'count(//root/item[starts-with(., "', 
        first(split(item(), '|')), 
        '") and normalize-space(substring-after(substring-after(., ";"), ";")) != "" and contains(., ";', 
        substring-before(substring-after(item(), '|'), ';'), 
        '")])'
    )
)

样本输入

"Data and Analytics|2024-09;2024-09-30;",
"Data and Analytics|2024-09;2024-09-30;",
"Enterprise App and Emerging Tech|2024-09;2024-10-01;",
"Enterprise App and Emerging Tech|2024-09;2024-10-01;",
"Mobility and Customer Experience|2024-09;2024-10-07;2024-12-31",
"Mobility and Customer Experience|2024-09;2024-10-07;2024-12-31",
"Mobility and Customer Experience|2024-10;2024-10-27;2024-11-07",
"Data and Analytics|2024-10;2024-10-23;2024-12-08",
"Data and Analytics|2024-10;2024-10-23;2024-12-08",
"Cloud - Infra - CS and Obser|2024-10;2024-10-24;2024-12-15"

补充的XML数据(@Fravadona编辑)

<root>
  <item>Data and Analytics|2024-09;2024-09-30;</item>
  <item>Data and Analytics|2024-09;2024-09-30;</item>
  <item>Enterprise App and Emerging Tech|2024-09;2024-10-01;</item>
  <item>Enterprise App and Emerging Tech|2024-09;2024-10-01;</item>
  <item>Mobility and Customer Experience|2024-09;2024-10-07;2024-12-31</item>
  <item>Mobility and Customer Experience|2024-09;2024-10-07;2024-12-31</item>
  <item>Mobility and Customer Experience|2024-10;2024-10-27;2024-11-07</item>
  <item>Data and Analytics|2024-10;2024-10-23;2024-12-08</item>
  <item>Data and Analytics|2024-10;2024-10-23;2024-12-08</item>
  <item>Cloud - Infra - CS and Obser|2024-10;2024-10-24;2024-12-15</item>
</root>

期望结果

类别月份统计数
Data and Analytics2024-090
Enterprise App and Emerging Tech2024-090
Mobility and Customer Experience2024-092
Mobility and Customer Experience2024-101
Data and Analytics2024-102
Cloud - Infra - CS and Obser2024-101

解决方案

原表达式问题分析

  1. 匹配逻辑不精准:用contains(., ";月份")可能匹配到第二个;之后的内容中的月份,导致误统计;starts-with(., "类别")未加|,会匹配到以该类别为前缀的其他条目。
  2. 变量混淆:Power Automate循环中的item()与XML节点<item>重名,可能导致XPath解析错误。

修正后的表达式

xpath(
    outputs('Compose_3'),
    concat(
        'count(//root/item[',
        'starts-with(., "', first(split(item(), '|')), '|")',
        ' and substring-before(substring-after(., "|"), ";") = "', substring-before(substring-after(item(), '|'), ';'), '"',
        ' and normalize-space(substring-after(substring-after(., ";"), ";")) != ""',
        '])'
    )
)

修正点说明

  • starts-with(., "类别|"):确保条目开头是目标类别加|,避免前缀匹配错误。
  • substring-before(substring-after(., "|"), ";") = "月份":精准提取|与第一个;之间的月份字符串,直接匹配,避免contains的误判。
  • 保留normalize-space(...) != "":确保第二个;之后的内容非空。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:35:54