Search code examples
xpathmuleesb

Using Conditional XPath in Mule ESB


My input XML is

<DatabaseValues>
<Value>
    <Sender>Satya</Sender>
    <WB_TRANSFORMATION>1</WB_TRANSFORMATION>
    <WB_VALIDATION>0</WB_VALIDATION>
    <WB_ENRICHMENT>0</WB_ENRICHMENT>
    <Receiver>Ch</Receiver>
</Value>
<Value>
    <Sender>Keerthika</Sender>
    <WB_TRANSFORMATION>1</WB_TRANSFORMATION>
    <WB_VALIDATION>0</WB_VALIDATION>
    <WB_ENRICHMENT>0</WB_ENRICHMENT>
    <Receiver>SS</Receiver>
</Value>

My requirement is to extract the <Receiver> value based on the corresponding <Sender> value. (eg. I need to extract "Ch" when Sender value is "Satya"). This should be achieved using an xpath expression.

I have tried the following

<set-variable variableName="satya" value="#[xpath3('/DatabaseValues/Value[1]/Sender')]" doc:name="Variable"/>

and then I did this

 #[xpath3('/DatabaseValues/Value[Sender="flowVars.satya"]/Receiver')]

There is no value being extracted. But when I used the below logic it worked

 #[xpath3('/DatabaseValues/Value[Sender="Satya"]/Receiver')]

My flow configuration is as follows

<file:connector name="File" autoDelete="true" streaming="false" validateConnections="true" doc:name="File"/>
<flow name="Sender_TransformationFlow">
    <file:inbound-endpoint path="C:\Users\Satyakeerthika_Ch\Desktop\Input" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <set-variable variableName="satya" value="#[xpath3('/DatabaseValues/Value[1]/Sender')]" doc:name="Variable"/>
    <set-variable variableName="xpath" value="#[xpath3('/DatabaseValues/Value[Sender= &quot;+ flowVars.satya +&quot;]/Receiver')]" doc:name="Variable"/>
    <logger message="#[flowVars.xpath]" level="INFO" doc:name="Logger"/>
    <logger message="#[flowVars.satya]" level="INFO" doc:name="Logger"/>
</flow>

I want to know how I can use the flowVars instead of a hard coded value in the Xpath to do check.
I am using Anypoint Studio version 5.1.0


Solution

  • For accessing flow vars from xpath3 function you should use '$' without 'flowVars'

    Take a look to the documentation

    https://docs.mulesoft.com/mule-user-guide/v/3.7/xpath#query-parameters

    In your example:

    <set-variable variableName="xpath" value="#[xpath3('/DatabaseValues/Value[Sender=$satya]/Receiver')]" doc:name="Variable"/>