XPATH in mule flow returns multiple values - mule

I have a requirement where I run xPath evaluator against xml payload in mule flow. This xPath evaluator can return single or multiple values. I need to store these values in flow variable and use somewhere later in the flow. Can someone help me implementing this changes?
Appreciate your help on this.
Thanks

For extracting values from an XML document, use the XPath extractor.
<mulexml:xpath-extractor-transformer expression="/a:my/b:xpath/text()"/>
You can also use Mule Expression Language to create dynamic XPath expressions:
<expression-transformer mimeType="text/xml" evaluator="xpath" expression="//school/day[#date= #[function:datestamp:yyyy-MM-dd] ]/name
"/>
However this can get somewhat messy for complicated expressions, so I have created my own dynamic XPath transformer:
<dx:dxpath expression="/b:team[name = $teamName]/b:player[b:name = $playerName]/b:goals/text()">
<dx:variable key="playerName" value="#[header:invocation:playerName]"/>
<dx:variable key="teamName" value="#[header:invocation:teamName]"/>
<!-- unlimited number of variables -->
</dx:dxpath>
which is somewhat more easy on the eyes.
Then Wrap your flow with an ericher:
<enricher target="#[variable:myData]">
<processor-chain>
<!-- your flow here -->
</processor-chain>
</enricher>

Related

how to replace xml element value in mule

<healthcare>
<plans>
<plan1>
<planid>100</planid>
<planname>medical</planname>
<desc>medical</desc>
<offerprice>500</offerprice>
<area>texas</area>
</plan1>
<plan2>
<planid>101</planid>
<planname>dental</planname>
<desc>dental</desc>
<offerprice>1000</offerprice>
<area>texas</area>
</plan2>
</plans>
</healthcare>
<splitter evaluator="xpath" expression="/healthcare/plans" doc:name="Splitter"/>
<transformer ref="domToXml" doc:name="Transformer Reference"/>
<logger level="INFO" doc:name="Logger" message=" plans detils...#[message.payload]" />
i have input xml data as above. I want to replace offerprice value from the above xml data . I tried various ways. anyone can shed the light for my requirement in mule
in my requiremnet , hit external api based on the result value , I need to change the offerprice value in the input xml .
anyone help is highly valuable.I need this immediately in my work .please shed light
You can use XSLT to transform the XML file to another XML file (with or without the same schema). Here's a small example of how it would look in mule . . .
http://marcotello.com/mule-esb/using-the-xslt-transformer-in-mule-esb/
There are also a lot of resources for learning how to create XSLT files online / through google.
There are many ways to do it.
You can use Mule XSLT in your flow, which will change the value of offerprice from input xml and you will be getting the required Xml as output with the value you want.
Another way is to use Groovy, with XmlSlurper to parse your input Xml, replace the value, and rebuild the XML you want.
reference :- XML Mapping in Mule
and
Mule: Enriching an XML with additional information from DB
also refer
http://www.ibm.com/developerworks/library/j-pg05199/index.html
Hope this help

Message Splitter

I needed to split a message into 3 different payloads and transform and send to 3 routers. So the payload initially will have a header a body or detail and a footer. These 3 different payloads need to be extracted and send to 3 different routers. What would be the most efficient way to do it.
It depends on your body/payload type. If your payload is XML, you can easily split it using xpath and route it using content based routing similar to:
<splitter expression="#[xpath('//nodes/node)']" />
<choice>
<when expression="#[xpath('//node/id').text ='myid']">
<!-- Route somewhere -->
</when>
<otherwise>
<!-- Route somewhere else -->
</otherwise>
</choice>
The expression splitter above can take any MEL expression to split up your payload. There are many other splitters, for example if your payload is already a java Collection, you can simply use the collection-splitter.
Other splitter info can be found here: http://www.mulesoft.org/documentation-3.2/display/32X/Message+Splitting+and+Aggregation
Also there are other routers that can help you with fork and join patterns if you need to process messages asynchronously as well. Here's a good post on that: http://java.dzone.com/articles/aggregation-mule-%E2%80%93-%E2%80%9Cfork-and

Mule Performing a string manipulation

What is the best way to perform a string manipulation. I wish to perform a substring on a email address to extract the domain detail and populate this to a variable.
a java transformer is a possibilty, but i was hoping if i could use a message enricher with a expression to perform this operation.
pardon me but i am still a greenhorn on Mule.
here is the excerpt from my mule flow which is failing with error cannot resolve method string length.
<enricher target="#[flowVars['FromAddressDomain']]" doc:name="Message Enricher">
<expression-transformer expression="#[ payload.fromAddr.substring(payload.fromAddr.lastIndexOf('#')+ 1,payload.fromAddr.lenth())]" doc:name="Expression"></expression-transformer>
</enricher>
Simply use:
<set-variable variableName="FromAddressDomain"
value="#[org.mule.util.StringUtils.substringAfter(payload.fromAddr, '#')]" />
You can use dataweave transform on payload and use the operator splitby and spilt on # character. Please take a look at below link for more information on splitby operator
https://docs.mulesoft.com/mule-user-guide/v/3.9/dataweave-operators#split-by

MULE expression-transformer not accepted

I'm trying to learn Mule ESB but get problems with example projects. Why are these lines
underlined red and not represented in the Message flow?
<expression-transformer name="returnAttachments">
<return-argument evaluator="attachments-list" expression="*.txt,*.ozb,*.xml" optional="false"/>
</expression-transformer>
I've cut and pasted these lines from mulesoft.org as part of a sample project.
#genjosanzo is right, the MEL equivalent would be:
<expression-transformer
expression="#[($.value in message.inboundAttachments.entrySet() if $.key ~= '(.*\\.txt|.*\\.ozb|.*\\.xml)')]" />
Mule studio has problem rendering nested elements (bug reported here)
Instead you can use the compact version and replace it with the following:
<expression-transformer expression="#[attachments-list:*.txt,*.ozb,*.xml]" doc:name="Expression" />
On a side note ever since mule 3.3.0 the new mule expression languages and it is recommended to rely on it whenever possible.

Mule choice compare

In mule flow I need to compare the last two params from an input string message
2012-04-30,2012-05-30,1,5
And if param1(1) <= param2(5), I need perform jms:outbound-endpoint.
Is there a standard choice element to do it? Could you please provide example.
While using mule 3.2.x your best option is to use the groovy evaluator and use a choice router that look like the following
<when expression="#[groovy:payload.split(',')[2] <= payload.split(',')[3] ]">
<processor-chain>
<jms:outbound-endpoint queue="out" doc:name="JMS"/>
</processor-chain>
</when>
Otherwise if you are willing to upgrade to Mule 3.3.0 you can take advantage of the new Mule Expression Language that allows to do the same without the need of the groovy operator
you can also use splitter and split on the basis of ',' and then have the compare logic for the params
You need not use Groovy for this. You can directly apply the split() function on payload.
<when expression="#[payload.split(',')[2]<=payload.split(',')[3]]">
<processor-chain doc:name="Processor Chain">
<jms:outbound-endpoint queue="data" connector-ref="Active_MQ"doc:name="JMS"/>
</processor-chain>
</when>
Apart from expression filter you can also use choice flow control. Capture the 2 values to be compared in a flowvars and use below expression in when condition of choice flow control.
[flowVars.param1 <= flowVars.param1]
Giving below the link for more information.
https://docs.mulesoft.com/mule-user-guide/v/3.7/choice-flow-control-reference
you can take advantage of the new Mule Expression Language that allows to do the same without the need of the groovy operator