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

如何使用XPath 1.0提取指定on-error-continue下的set-variable节点

XPath 1.0 提取指定节点解决方案

需求场景

需要基于给定的XML内容,用XPath 1.0提取符合规则的子节点。

给定XML内容

<on-error-continue type="APIKIT:BAD_REQUEST" enableNotifications="true" logException="true">
 <set-variable value="200" doc:name="httpStatus" variableName="httpStatus" />
 <set-variable value="Bad request" doc:name="logDescription" variableName="logDescription" />
 <flow-ref doc:name="global-prepare-error-response-sub-flow" name="global-prepare-error-response-sub-flow"/>
</on-error-continue>
<on-error-continue type="APIKIT:TOO_MANY_REQUEST" enableNotifications="true" logException="true">
 <set-variable value="200" doc:name="httpStatus" variableName="httpStatus" />
 <set-variable value="Many request" doc:name="logDescription" variableName="logDescription" />
 <flow-ref doc:name="global-prepare-error-response-sub-flow" name="global-prepare-error-response-sub-flow"/>
</on-error-continue>

提取规则

  • 父节点为type属性等于APIKIT:BAD_REQUEST的on-error-continue节点
  • 子节点为value属性等于200、variableName属性等于httpStatus的set-variable节点

预期提取结果

"set-variable value="200" doc:name="httpStatus" variableName="httpStatus"

原表达式问题

原有适配XPath 2.0的表达式如下,无法在XPath 1.0中运行:

//*[local-name()='on-error-continue'][@*[local-name()='type' and .='APIKIT:BAD_REQUEST']]/set-variable[@value='200' and @variableName='httpStatus']

问题原因是XPath 1.0不支持在属性筛选条件中嵌套节点值判断的写法。

正确XPath 1.0表达式

兼容命名空间版本(推荐,适配任意命名空间场景)

//*[local-name()='on-error-continue' and @*[local-name()='type']='APIKIT:BAD_REQUEST']/*[local-name()='set-variable' and @value='200' and @variableName='httpStatus']

无命名空间简化版本

如果确定XML没有自定义命名空间,可以使用更简洁的写法:

//on-error-continue[@type='APIKIT:BAD_REQUEST']/set-variable[@value='200' and @variableName='httpStatus']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 15:39:02