Reputation: 69
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
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()="TestAccepted"]/@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
Reputation: 4303
Try with this..
<when expression="#[output application/java --- XmlModule::xpath('//*[local-name()="TestAccepted"]/@accepted',payload.^raw,{})[0] == 'OWD']">
Upvotes: 0
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'
}
Upvotes: 0