MEL XPath3 expression for attribute - mule

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\']')

Related

JSON parsing for a particular element

Below xpath expression looks for AccountNo element anywhere in whole xml document.
#[xpath3('//AccountNo').text]
I am after similar expression to get AccountNo from a json request if present anywhere.
Below are example xml request containing AccountNo.
<Account>
<AccountName>John</AccountName>
<AccountNo>4234324</AccountNo>
</Account>
<Order>
<OrderId>34234242</OrderId>
<ServiceOrder>
<AccountNo>231232</AccountNo>
<ServiceOrderId>54654698787</ServiceOrderId>
</ServiceOrder>
<ServiceOrder>
<AccountNo>231232</AccountNo>
<ServiceOrderId>78979879797</ServiceOrderId>
</ServiceOrder>
</Order>
Thanks in advance for any help.
To get the value from a JSON payload element in Mule flow you first need to convert the json payload to object as below.
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
Then fetch the json payload element as below. In below code I am fetching the value of RecordId from json payload.
<set-variable variableName="RecordID" value="#[payload.DocProcessingMessage.UploadEfnolDocument.RecordId]" doc:name="Variable_RecordID"/>
Once you are done, Convert the payload back to JSON for further processing.
<json:object-to-json-transformer doc:name="Object to JSON"/>
First convert xml payload to Json then process the payload using transformer
<json:xml-to-json-transformer doc:name="XML to JSON" mimeType="application/json"/>
<set-payload value="{ "AccountNo": "#[json:Account:AccountNo]"}" mimeType="application/json" doc:name="payload"/>

how to replace value using xpath3 expression in mule esb

<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/

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