Simbu
Simbu

Reputation: 796

How to process response in mule ESB

I am trying to write data weave to add the price in the response xml.

Request xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<UniversityResponse xmlns="urn:abc:api:Components">
    <Timestamp>2015-12-16T22:05:41.229Z</Timestamp>
    <Dept>IT</Dept>
    <Fees>
        <Fee>
            <Name>John</Name>
            <Fee currencyID="USD">1.0</Fee>
        </Fee>
        <Fee>
            <Name>Enrique</Name>
            <Fee currencyID="USD">3.0</Fee>
        </Fee>
        <Fee>
            <Name>Mary</Name>
            <Fee currencyID="USD">2.0</Fee>
        </Fee>
        <Fee>
            <Name>Meena</Name>
            <Fee currencyID="USD">0.5</Fee>
        </Fee>
    </Fees>
</UniversityResponse>

As of now i wrote like

%dw 1.0
%namespace ns0 urn:abc:api:Components
%output application/xml
---
TotalFees:{
    plus : payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee[0].ns0#Fee +
    payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee[0].ns0#Fee +
    payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee[1].ns0#Fee +
    payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee[2].ns0#Fee +
    payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee[3].ns0#Fee 
}

Output :

<TotalFees>
  <plus>6.5</plus>
</TotalFees>

How Can i loop the complex tag in the response xml ?

Basically i want to create one loop for UniversityResponse/Fees. Inside the loop i want to write something like Plus = Plus + "UniversityResponse/Fees/Fee[counter].Fee"

Upvotes: 1

Views: 89

Answers (1)

JoostD
JoostD

Reputation: 744

That was kind of a headbreaker because i was looking for a SUM function in the docs, not in the docs but it is in the language :)

--

%dw 1.0
%output application/xml
%namespace ns0 urn:abc:api:Components
---

TotalFees: plus: sum payload.ns0#UniversityResponse.ns0#Fees.*ns0#Fee.ns0#Fee

<?xml version='1.0' encoding='UTF-8'?>
<TotalFees>
  <plus>6.5</plus>
</TotalFees>

Upvotes: 1

Related Questions