'how to use variable inside <xml-module:xpath-extract> mule 4
I have a variable set value as "Active." Based on the value of a variable, I want to select a node in the payload.
If I give values directly in xpath, it's work.
When I'm trying to use variables inside an xpath, like @Status = vars.xstatus. It returns a null value [].
Mule 4 flow:
<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 expression:
/DTOApplication/DTOLocation[@Status='Active']
XML payload: https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml
how to use variables in XPath Mule 4?
Solution 1:[1]
I have edited your code, use below code that is having corrections for the xpath expression
<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>
Below is the screenshot that is showing how and where I did a correction
In the above screenshot as I Mentioned that click on "fx", if you add the expression without clicking on it and run your code, you will get an error as below
"Could not compile xpath expression "/DTOApplication/DTOLocation[@Status=" ++ vars.xstatus ++ "]""
In Mule 3, you must learn both the Mule Expression Language (MEL) and DataWeave. but now in Mule-4 DataWeave is the default expression language, if you click on "fx" it means you are entering DataWeave Expression Language
After this correction, I am getting correct output as in the below snippet
below is the complete xml file
<?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>
Note: I am using Anypoint Studio version 7.11.1 ad Mule runtime version -4.4.0 (but I tried with runtime 4.3.0 and its working fine)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |