数据编辑中的xml输入映射

时间:2016-03-17 07:33:25

标签: xml mule dataweave anypoint-studio

输入文件是 -

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

尝试使用dataweave中的以下代码进行映射

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

我正在尝试映射A下的B的每个值,但是每次我得到相同的第一个东西5次作为数量1.0是否应该更改为2.0 4.0和5.0

请纠正我如果我做错了什么来实现B的所有价值

2 个答案:

答案 0 :(得分:0)

如果使用mapObject而不定义参数(键和值),则应使用其默认值。密钥默认定义为 $$ ,值为 $

因此,DataWeave代码的(部分)应为:

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

答案 1 :(得分:0)

我还没有看到为什么$ .amount无法访问,但在代码中进行一些更改会使其工作。检查下面 -

%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
                       }
                     }
           }
     }
}

输出看起来像 -

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