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 Analytics | 2024-09 | 0 |
| Enterprise App and Emerging Tech | 2024-09 | 0 |
| Mobility and Customer Experience | 2024-09 | 2 |
| Mobility and Customer Experience | 2024-10 | 1 |
| Data and Analytics | 2024-10 | 2 |
| Cloud - Infra - CS and Obser | 2024-10 | 1 |
解决方案
原表达式问题分析
- 匹配逻辑不精准:用
contains(., ";月份")可能匹配到第二个;之后的内容中的月份,导致误统计;starts-with(., "类别")未加|,会匹配到以该类别为前缀的其他条目。 - 变量混淆: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
相关产品推荐
相关产品推荐

