如何在 <xml-module:xpath-extract> mule 4 中使用变量

how to use variable inside <xml-module:xpath-extract> mule 4

我有一个变量设置值为“有效”。基于变量的值,我想 select 有效载荷中的一个节点。

如果我直接在 xpath 中给出值,就可以了。

当我尝试在 xpath 中使用变量时,例如 @Status = vars.xstatus。它 returns 一个空值 [].

Mule 4 流量:

<flow name="var_xpath_testFlow" doc:id="406270fb-17e7-48e9-a33a-f7a0197f8e05" >
        <http:listener doc:name="Listener" doc:id="662f2277-859f-4516-974b-de7cceeb5b40" config-ref="HTTP_Listener_config" path="/vartest"/>
        <set-variable value="Active" doc:name="Set Variable" doc:id="12370458-bb32-4709-b509-acc1e5f50b94" variableName="xstatus"/>
        <xml-module:xpath-extract doc:id="6027ebad-f81d-4df1-8234-3d8dab04b129" config-ref="XML_Config" xpath="/DTOApplication/DTOLocation[@Status=vars.xstatus ]" target="xvalue"/>
        <logger level="INFO" doc:name="Logger" doc:id="f495b0bf-f8cb-43be-b4d9-d069758e4028" message="#[vars.xvalue]"/>
</flow>

XPATH 表达式:

/DTOApplication/DTOLocation[@Status='Active']

XML 负载:https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml

如何在 XPath Mule 4 中使用变量?

使用 Mule 4 表达式代替文字字符串。

类似于:

xpath="#[&quot;/DTOApplication/DTOLocation[@Status='&quot; ++ vars.xstatus ++ &quot;']&quot;]"/>

我已经编辑了您的代码,请使用以下对 xpath 表达式进行更正的代码

<flow name="var_xpath_testFlow" doc:id="7490a882-4473-461d-846b-b6a7b65a8eca" >
        <http:listener doc:name="Listener" doc:id="4767ed08-42cb-467c-80c2-4b739c68724e" config-ref="HTTP_Listener_config" path="/vartest"/>
        <set-variable value="Active" doc:name="Set Variable" doc:id="62eaffcf-ea2e-4f05-8ad8-d840bbebe9e4" variableName="xstatus"/>
        <xml-module:xpath-extract doc:id="15124867-b70c-4656-8eb9-d99d4e500a0e" config-ref="XML_Config" xpath='#["/DTOApplication/DTOLocation[@Status=" ++ vars.xstatus ++ "]"]' target="xvalue"/>
        <logger level="INFO" doc:name="Logger" doc:id="340b9b89-18f8-4584-b6cb-7ab9d6d8efed" message="#[vars.xvalue]"/>
</flow>

下面是显示我如何以及在何处进行更正的屏幕截图

在上面的屏幕截图中,我提到点击“fx”,如果您添加表达式而不点击它和运行您的代码,您将收到如下错误

"Could not compile xpath expression "/DTOApplication/DTOLocation[@Status=" ++ vars.xstatus ++ "]""

在 Mule 3 中,您必须学习 Mule 表达式语言 (MEL) 和 DataWeave。但是现在在 Mule-4 中 DataWeave 是默认的表达式语言,如果你点击“fx”就意味着你正在进入 DataWeave 表达式语言

更正后,我得到了正确的输出,如下面的代码片段所示

下面是完整的 xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<mule
    xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:x12="http://www.mulesoft.org/schema/mule/x12"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/x12 http://www.mulesoft.org/schema/mule/x12/current/mule-x12.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml-module http://www.mulesoft.org/schema/mule/xml-module/current/mule-xml-module.xsd">
    <http:listener-config name="HTTP_Listener_config"
        doc:name="HTTP Listener config"
        doc:id="5fe97da4-b1a9-4a9b-9a23-c4c42249fb2b">
        <http:listener-connection host="0.0.0.0"
            port="8081" />
    </http:listener-config>
    <xml-module:config name="XML_Config"
        doc:name="XML Config" doc:id="6c8afa4c-6fec-4791-94a3-b4f8f63442e7" />

    <flow name="var_xpath_testFlow"
        doc:id="7490a882-4473-461d-846b-b6a7b65a8eca">
        <http:listener doc:name="Listener"
            doc:id="4767ed08-42cb-467c-80c2-4b739c68724e"
            config-ref="HTTP_Listener_config" path="/vartest" />
        <set-variable value="Active" doc:name="Set Variable"
            doc:id="62eaffcf-ea2e-4f05-8ad8-d840bbebe9e4" variableName="xstatus" />
        <xml-module:xpath-extract
            doc:id="15124867-b70c-4656-8eb9-d99d4e500a0e" config-ref="XML_Config"
            xpath='#["/DTOApplication/DTOLocation[@Status=" ++ vars.xstatus ++ "]"]'
            target="xvalue" />
        <logger level="INFO" doc:name="Logger"
            doc:id="340b9b89-18f8-4584-b6cb-7ab9d6d8efed"
            message="#[vars.xvalue]" />
    </flow>
</mule>

注意:我使用的是 Anypoint Studio 版本 7.11.1 ad Mule 运行time 版本 -4.4.0(但我尝试使用 运行time 4.3.0 并且工作正常)