首页 > 解决方案 > 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>

想拿到单曲

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

使用 xPath 1.0 表达式:父级是 -->on-error-continue type="APIKIT:BAD_REQUEST",子级是 -->set-variable value = "200"。

试过下面的表达。它适用于 Xpath2.0 但不适用于 1.0

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

标签: xpath

解决方案


使用这个方便的网站,我将 xml 放入根元素中,<root>YOUR XML</root>.

使用这个 XPath:

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

我能够提取匹配的记录。自己试试,把上面 XPath 中的root替换成。*您应该看到您正在寻找的记录。

通配符运算符可以像路径中的任何元素一样使用。


推荐阅读