Abhilash Khajuria
Abhilash Khajuria

Reputation: 69

Mule 4 Xpath Expression With When Condition

I am trying to add when expression based on xml input using xpath of mule 4 #[XmlModule::xpath] Below is my input

<TestAccepted xmlns="http://api.gsicommerce.com/schema/checkout/1.0"
accepted="OWD">
<Test userId="1234">
    <Name>
        <LastName>TestName</LastName>
        <FirstName>TestName</FirstName>
    </Name>        
</Test>

I want to add a when condition expression based on

#[XmlModule::xpath(//TestAccepted.@accepted == 'OWD')]

Kindly help on this. My old logic on mule 3 is working. Facing difficulty to find mule 4 solution.

Upvotes: 1

Views: 1606

Answers (3)

aled
aled

Reputation: 25812

I strongly recommend to use DataWeave instead of XPath, like in the @Alex solution. Even the documentation of the XML Module recommends to use DataWeave.

If you really really want to still use XPath, then you can use this expression:

#[XmlModule::xpath('//*[local-name()=&quot;TestAccepted&quot;]/@accepted', write(payload, 'application/xml'), {}) contains 'OWD']

On problem was that for some reason the payload was interpreted as an object instead of XML. I used an explicit write() to force the XML. Then the result of XmlModule::xpath() function is an array of results, so I used contains() to see if the result is in there.

Upvotes: 0

Salim Khan
Salim Khan

Reputation: 4303

Try with this..

<when expression="#[output application/java --- XmlModule::xpath('//*[local-name()=&quot;TestAccepted&quot;]/@accepted',payload.^raw,{})[0] == 'OWD']">

Upvotes: 0

Alex
Alex

Reputation: 4473

I believe xpath is deprecated in Mule 4. Use regular mapping techniques:

%dw 2.0
var x=read('<TestAccepted xmlns="http://api.gsicommerce.com/schema/checkout/1.0" accepted="OWD">
<Test userId="1234">
    <Name>
        <LastName>TestName</LastName>
        <FirstName>TestName</FirstName>
    </Name>        
</Test></TestAccepted>','application/xml')
output application/json
---
{
    result: x.TestAccepted.@accepted,
    boolean: x.TestAccepted.@accepted ~= 'OWD'
}

enter image description here

Upvotes: 0

Related Questions