Resolved: xpath 3 to XmlModule::xpath() in mule 4

Question:

I’m trying to migrate from Mule 3 to Mule 4.
XPATH3 is not supported in Mule 4.
I have xpath in Mule3:
xpath3('/DTOApplication/DTOLocation[@Status=\'Active\']',flowVars.domPayload,'NODESET')
Mule 3 flow:
<flow name="xpath_transformFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/stepnode" allowedMethods="POST" doc:name="HTTP"/>
<set-variable variableName="dtolocation" value="#[xpath3('/DTOApplication/DTOLocation[@Status=\'Active\']',flowVars.domPayload,'NODESET')]" doc:name="set dtolocation"/>
<logger message="#[flowVars.dtolocation]" level="INFO" doc:name="Logger"/>
</flow>
xml payload : https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml
I am trying xpath in mule 4 using #[XmlModule::xpath()] but its shows error.
Error message:
Script 'XmlModule::xpath('/DTOApplication/DTOLocation[@Status=\'Active\'],vars.domPayload,
{}) ' has errors:   Invalid input ''', expected ')' for the function
call. (line 1, column 18):  at 1 : 1" evaluating expression:
"#[XmlModule::xpath('/DTOApplication/DTOLocation[@Status=\'Active\'],vars.domPayload,
{})]".
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="#[payload]" doc:name="Set domPayload" doc:id="2495ac98-b976-41e0-9dcf-398574e54ffa" variableName="domPayload"/>
        <set-variable value="#[XmlModule::xpath('/DTOApplication/DTOLocation[@Status=\'Active\'],vars.domPayload, {})]" doc:name="Set dtolocation_value" doc:id="3c751150-3606-4c40-9748-e658d9e6e59e" variableName="dtolocation_value"/>
        <logger level="INFO" doc:name="Logger" doc:id="f495b0bf-f8cb-43be-b4d9-d069758e4028" message="#[vars.dtolocation_value]"/>
</flow>
How can I use it in mule 4?

Answer:

The invocation to xpath() was missing a closing quote in the first parameter, after the first closing bracket. It looks like it is fixed in the expression but the error message is not updated.
Anyway after that I had an error because the function expects a Binary as the second argument. In my test the xpath-extract operation worked fine:
<xml-module:xpath-extract xpath="/DTOApplication/DTOLocation[@Status='Active']" doc:name="Xpath extract"/>
As an alternative I prefer to use DataWeave instead:
%dw 2.0
output application/xml
---
payload.DTOApplication filterObject ($$ as String == "DTOLocation" and [email protected] == "Active")
Note that if you are using the output to perform further processing it would be preferred to output application/java.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

If you like this answer, you can give me a coffee by click here (view Ads)