如何使用XPATH筛选含detection属性的JSON对象的指定字段
Power Automate中XML的XPATH查询解决方案
需求1:仅提取包含detection节点的对象中的service_name
你原有的XPATH会返回所有service_name,问题出在没关联“存在detection节点”这个过滤条件。要实现精准提取,先定位到包含detection子节点的对象,再从中获取service_name,使用以下语句:
xpath(xml(variables('varProductsRoot')), '//*[detection]/remediation/packages/service_name')
逻辑说明:
//*[detection]:匹配所有包含detection子节点的元素(对应原JSON里root下各数组中带detection的对象)/remediation/packages/service_name:从匹配到的元素里,逐层定位到目标service_name节点
需求2:单个XPATH同时返回符合条件的service_name与detection
通过带关联条件的路径组合实现,确保返回的字段都来自包含detection的对象:
xpath(xml(variables('varProductsRoot')), '//*[detection]/remediation/packages/service_name | //*[detection]/detection')
逻辑说明:
- 用
|连接两个路径,第一个路径提取符合条件的service_name,第二个路径提取对应的detection节点 - 两个路径都基于
//*[detection]过滤,保证返回内容的关联性
效果验证
以你提供的JSON为例,转换为XML后:
- 需求1会返回:IPS、Web App Security、Vulnerability、IPS、Web App Security(自动过滤掉无detection对象的service_name)
- 需求2会返回上述service_name,以及对应的detection节点内容(包含attackid、signature_id等子节点)
内容的提问来源于stack exchange,提问作者sugarakis
相关产品推荐
相关产品推荐

