简体   繁体   中英

xml input mapping in dataweave

input file is-

 <A> <B> <merchant_ref>icici</merchant_ref> <transaction_tag>sdfhisdb</transaction_tag> <transaction_type>inquiry</transaction_type> <method>valuelink</method> <order_number>123</order_number> <amount>1000</amount> <currency_code>CAD</currency_code> </B> <B> <merchant_ref>icici</merchant_ref> <transaction_tag>sdfhisdb</transaction_tag> <transaction_type>inquiry</transaction_type> <method>valuelink</method> <order_number>123</order_number> <amount>2000</amount> <currency_code></currency_code> </B> <B/> <B> <merchant_ref>icici</merchant_ref> <transaction_tag>sdfhisdb</transaction_tag> <transaction_type>inquiry</transaction_type> <method>valuelink</method> <order_number>123</order_number> <amount>4000</amount> <currency_code></currency_code> </B> <B> <merchant_ref>icici</merchant_ref> <transaction_tag>sdfhisdb</transaction_tag> <transaction_type>inquiry</transaction_type> <method>valuelink</method> <order_number>123</order_number> <amount>5000</amount> <currency_code></currency_code> </B> </A> 

tried to map using the below code in dataweave

  %output application/xml --- Inquiry: payload.A.*B mapObject { balanceInquiry: { request: { amount:{ amount: payload.ABamount/1000 as :number, currency: payload.ABcurrency_code } } } } 

I am trying to map each value of B which is under A but every time I get the same first thing 5 times as amount 1.0 whether it should change to 2.0 4.0 and 5.0

Please correct me If I am doing something wrong to achieve all the values of B

If using mapObject and not define the parameter (key and value), then you should use its default. The key is defined by default as $$ and the value as $ .

Therefore, the (part of) DataWeave code should be:

amount: $.amount / 1000 as :number when $.amount != null otherwise 0,
currency: $.currency_code default ""

I haven't looked more why $.amount is not accessible but making some change in your code would make it work. Check below -

%dw 1.0
%output application/xml
---
{
 Inquiry: payload.A.*B mapObject
    {
        balanceInquiry:
           { 
            request: {
                amount:{
                amount: ($[5]/1000 when $[5] != null otherwise 0) as :number,            
                currency: payload.A.B.currency_code
                       }
                     }
           }
     }
}

Output looks like -

<?xml version='1.0' encoding='windows-1252'?>
<Inquiry>
  <balanceInquiry>
    <request>
      <amount>
        <amount>1.0</amount>
        <currency>CAD</currency>
      </amount>
    </request>
  </balanceInquiry>
  <balanceInquiry>
    <request>
      <amount>
        <amount>2.0</amount>
        <currency>CAD</currency>
      </amount>
    </request>
  </balanceInquiry>
  <balanceInquiry>
    <request>
      <amount>
        <amount>0</amount>
        <currency>CAD</currency>
      </amount>
    </request>
  </balanceInquiry>
  <balanceInquiry>
    <request>
      <amount>
        <amount>4.0</amount>
        <currency>CAD</currency>
      </amount>
    </request>
  </balanceInquiry>
  <balanceInquiry>
    <request>
      <amount>
        <amount>5.0</amount>
        <currency>CAD</currency>
      </amount>
    </request>
  </balanceInquiry>
</Inquiry>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM