how to replace value using xpath3 expression in mule esb - mule

<babyproducts>
<products>
<product1>
<productid>100</productid>
<productname>towel</productname>
<desc>towel</desc>
<discount>100</discount>
<validlity>10</validlity>
</product1>
<product2>
<productid>101</productid>
<productname>pillow</productname>
<desc>pillow</desc>
<discount>500</discount>
<validlity>5</validlity>
</product2>
</product>
</babyproducts>
I want to split above xml using xpath3 expression and replace the validlity value . after that i want whole set of above xml replaced values. I tried using xpath it is working fine. my requirement insists on doing using xpath3 mule expression
<splitter evaluator="xpath" expression="/babyproducts/products" doc:name="Splitter" />
<set-payload value="#[message.payload]" doc:name="Set Payload" />
<enricher source="#[flowVars.newvalidlity]" target="#[xpath('/products/validlity').text ]" doc:name="Message Enricher">
<logger level="INFO" message=" enricher done ~~~~~.." doc:name="Logger" />
</enricher>
above is my mule flow using xpath. my team wants to do using xpath3 (latest).
my expected outpu is like below with new validlity value 50 (comes from flow variable)
<babyproducts>
<products>
<product1>
<productid>100</productid>
<productname>towel</productname>
<desc>towel</desc>
<discount>100</discount>
<validlity>50</validlity>
</product1>
<product2>
<productid>101</productid>
<productname>pillow</productname>
<desc>pillow</desc>
<discount>500</discount>
<validlity>50</validlity>
</product2>
</product>
</babyproducts>

First of all your XML sample is invalid. There are unclosed tags and your xpath doesn't match the structure of the sample either.
Second of all I can only show the equivalent xpath3 expression of your xpath expression as I have already answered how to update an XML node in other questions of yours.
<splitter evaluator="xpath" expression="/babyproducts/products" doc:name="Splitter" />
Becomes:
<splitter expression="#[xpath3('/babyproducts/products/*', payload, 'NODESET')]"
doc:name="Splitter" />
And
#[xpath('/products/validlity').text]
Becomes:
#[xpath3('/products/validlity')]
No need for .text as by default returns the Stirng value. More info here: http://blogs.mulesoft.org/mule-3-6-xml-xpath-xslt-xquery3/

Related

MEL XPath3 expression for attribute

I am splitting an xml document and below is a sample
<Row>
<tags id="1165" name="Code_Name">pqr</tags>
<tags id="1102" name="Source_Name">xxx</tags>
<tags id="1176" name="Code_Value">12_M</tags>
<tags id="1177" name="Code_Value_Description">Annual</tags>
<tags id="1148" name="System_Control">R</tags>
<tagi id="1169" name="Code_Instance">10278</tagi>
<tags id="10494" name="Has_History"/>
<tags id="10495" name="Obsolete"/>
<tagi id="1179" name="Code_Value_Instance">1545</tagi>
<tagi id="1168" name="Source_Number">4</tagi>
<tags id="1106" name="Update_Date">20150821</tags>
<tags id="944" name="Update_Source">abc</tags>
</Row>
I need to extract the value of id 1165. I am trying a MEL XPath3 expression and getting errors.
#[xpath3('/Row/tags/#id='1165'')]
I know #[xpath3('/Row/tags[1]')] works but I need select based on the attribute id.
Fragment of the flow is below.
<foreach collection="xpath3('/soapenv:Envelope/soapenv:Body/cmn:Response/Report/Level/Tab/*', payload, 'NODESET')" doc:name="For Each">
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="PL3 #[xpath3('/Row/tags/#id='1165'')] #[xpath3('/Row/tags[1]')]" level="INFO" doc:name="Logger"/>
</foreach>
Any help would be greatly appreciated.
Regards,
Hari
xpath3 is namespace-aware so you need to provide that. But you can use wildcard as below. In addition, you need to escape the single quotes.
xpath3('/*:Row/*:tags[#id=\'1165\']')

how to enrich xml node value in mule [duplicate]

This question already has an answer here:
replace xml particular node element value with other value in mule
(1 answer)
Closed 7 years ago.
input xml like below
<healthcare>
<plans>
<plan1>
<planid>100</planid>
<planname>medical</planname>
<desc>medical</desc>
<offerprice>3000</offerprice>
<area>texas</area>
</plan1>
<plan2>
<planid>101</planid>
<planname>dental</planname>
<desc>dental</desc>
<offerprice>4000</offerprice>
<area>texas</area>
</plan2>
</plans>
</healthcare>
<set-variable variableName="newoffer" value="2000" doc:name="Variable" />
<foreach doc:name="For Each" collection="#[xpath3('//healthcare/plans', message.payload, 'NODESET' )]">
<enricher source="#[flowVars.newoffer]" target="#[xpath3('offerprice', payload 'STRING')]" >
<logger message="...#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
</enricher>
<logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
</foreach>
i want enrich xml element offerprice value as 2000 .but above doesn't do that. anybody have any suggestions. main thing is my expected output is like below. I'm new to mule .anyone shed light for my mule requirement.thanks in advance
<healthcare>
<plans>
<plan1>
<planid>100</planid>
<planname>medical</planname>
<desc>medical</desc>
**<offerprice>2000</offerprice>**
<area>texas</area>
</plan1>
<plan2>
<planid>101</planid>
<planname>dental</planname>
<desc>dental</desc>
**<offerprice>2000</offerprice>**
<area>texas</area>
</plan2>
</plans>
</healthcare>
To achieve your goal, remove the for-each / enricher elements and instead use the XSLT Transformer that comes bundled with Mule. This is your best choice for transforming XML to XML.

mule xpath3 not picking up the value

<splitter evaluator="xpath" expression="/healthcare/plans" doc:name="Splitter" />
<enricher source="#[flowVars.tempid]" target="#[xpath('/plans/planid').text ]" doc:name="Message Enricher">
above code working fine using xpath
but below code using xpath3 not picking up the text
<splitter expression="#[xpath3('/healthcare/plans', payload, 'NODESET')]" doc:name="Splitter" />
<enricher source="#[flowVars.tempid]" target="#[xpath3('/plans/planid').text ]" doc:name="Message Enricher">
Theres a few things wrong with your config:
By default xpath3 returns a String, so no need to call .text. More on this here: http://blogs.mulesoft.org/mule-3-6-xml-xpath-xslt-xquery3/
The source and target expressions the wrong way round. If you want to store the result of the XPath expression in a a variable then target is where the variable should be and source is the source of the value in this case the xpath expression.
Your Xpath expression is wrong. No need for /plans as the XML is already split into individual plan nodes.
Try this:
<splitter expression="#[xpath3('/healthcare/plans/*', payload, 'NODESET')]"
doc:name="Splitter" />
<enricher source="#[xpath3('planid')]" target="#[flowVars.tempid]"
doc:name="Message Enricher">

How to perform string operation on JSON MEL in mule esb

I want to perform some string operation on MEL I have following expression in MEL
<logger message="#[json:xy/PID/xy.3/AC]" level="INFO" doc:name="Logger"/>
OUTPUT IS
19901026000000
I want to extract 1st 4 digit then 6,7 digit .
How can I do this ??
Thanks
What about trying it in two steps?
<set-variable variableName="result" value="#[json:ADT_A01/PID/PID.3/CX.1]" />
<set-variable variableName="result" value="#[result.substring(0,4)]#[result.substring(5,7)]" />
As noted in the comments in #Ryan Hoegg answer, the JSON expression evaluator has been deprecated since Mule 3.3 and hence the best way to do this would be to use a json to object transformer
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
and then use conventional MEL to traverse the Map
JsonPath expression are depreciated for now and you will even not get enough document on it for doing ..
So, currently you need to use either :- <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
or <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />
or even <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" /> to extract data from JSON depending on the JSON data

SMTP subject expression evaluation fails

I have a flow where I want to evealuate an expression on the smtp subject attribute but always get mvel parse expression though the logger give me the right values.
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="MYRequestPayloadID #[json:RequestPayloadID] #[json:ResponseStatusCd]" level="DEBUG" doc:name="Logger"/>
<smtp:outbound-endpoint host="${mail.host}" to="${mail.to}" from="${mail.from}" subject="Error Response for PayloadID #[json:RequestPayloadID], Status #[json:ResponseStatusCd]" responseTimeout="10000" doc:name="SMTP />
I am not sure why in the subject #[json:RequestPayloadID] and #[json:ResponseStatusCd] evaluation fail though I get the data back in the logger . Thanks.
Expressions are not supported everywhere in Mule (alas): this is one place where it isn't.
You have to use message properties to set the subject dynamically:
<set-property propertyName="subject"
value="Error Response for PayloadID #[json:RequestPayloadID], Status #[json:ResponseStatusCd]" />
FTR json: is the old expression style, nowadays you should be using MEL instead: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips#MuleExpressionLanguageTips-JSONProcessing