0
votes

I have a requirement to retrieve AmountRequired depending on the category from the below input and list out all the repeated elements.

<root>
    <Unspecified>
        <Category>T03</Category>
        <AmountRequired>993</AmountRequired>
        <ExcessAmount>250</ExcessAmount>
        <Currency>GBP</Currency>
        <ExchangeRate>1</ExchangeRate>
    </Unspecified>
    <Unspecified>
        <Category>T03</Category>
        <AmountRequired>9903</AmountRequired>
        <ExcessAmount>250</ExcessAmount>
        <Currency>GBP</Currency>
        <ExchangeRate>1</ExchangeRate>
    </Unspecified>
    <Unspecified>
        <Category>P02</Category>
        <AmountRequired>992</AmountRequired>
        <Currency>GBP</Currency>
        <ExchangeRate>1</ExchangeRate>
    </Unspecified>
    <Unspecified>
        <Category>A29</Category>
        <AmountRequired>929</AmountRequired>
        <Currency>GBP</Currency>
        <ExchangeRate>1</ExchangeRate>
    </Unspecified>
</root>

I want to get the out put as below. When there are 2 tags of T03 its should display both the amounts as below. Also if a Category doesn't exist it doesn't for example P05 and if we have the transformation for it then it errors out that P05 doesn't exist, so is there a way to get over the error even if the input xml doesn't contain the P05 Category?

<amountRequired>
        <amount>929</amount>
        <currency>GBP</currency>
        <exchangeRate>1</exchangeRate>
        <amountInSystemCurrency>550</amountInSystemCurrency>
    </amountRequired>
<amountRequired>
    <amount>993</amount>
    <currency></currency>
    <exchangeRate></exchangeRate>
    <amountInSystemCurrency></amountInSystemCurrency>
</amountRequired>
<amountRequired>
    <amount>9903</amount>
    <currency></currency>
    <exchangeRate></exchangeRate>
    <amountInSystemCurrency></amountInSystemCurrency>
</amountRequired>
<amountRequired>
        <amount>992</amount>
        <currency></currency>
        <exchangeRate></exchangeRate>
        <amountInSystemCurrency></amountInSystemCurrency>
    </amountRequired>

Highly appreciate any help with this

1

1 Answers

0
votes

you can use below code -

%dw 1.0
%output application/xml
---
using (T03=payload.root.*Unspecified filter ($.Category == 'T03')){
    //http://stackoverflow.com/questions/36598550/mule-dataweave-using-when-and-map
    amountRequired:{  
    amount:T03.AmountRequired[0] default 0,
    currency:"",
    exchangeRate:"",
    amountInSystemCurrency:""
} 
}

It uses a variable T03 to hold the T03 category item filtered from original payload. amount: T03.AmountRequired[0] default 0 will output amount from T03 when exist otherwise default 0.

In comments to this answer, you seem to have changed the requirement :). Anyways, editing this answer for to accommodate below comment "
I have edited the question, hope it makes sense. I want to output all tags (note:T03 is repeated and want to output both of them) – Neil24 13 mins ago"

%dw 1.0
%output application/xml
---
{
    root : { (payload.root.*Unspecified map 
        amountRequired: {
            amount:$.AmountRequired unless $.Category != 'T03' otherwise 0,
            currency:"",
            exchangeRate:"",
            amountInSystemCurrency:""
        }
    )}
}

This is very straight forward transformation and amount field is 0 unless the current category is T03. In original question you had mentioned that you need amount only if its a T03 category so there is unless .. otherwise check, if you want amount for every category, just remove it.

Hope That Helps!