在DataWeave中映射嵌套地图时出现问题

时间:2018-10-08 16:26:45

标签: mule mule-component dataweave mule-esb

我从Web服务获得以下xml响应,我想要实现的如下: 1.从响应中找到最大的SEQ_NO。 2.如下所示,将响应设置为预期的格式,但是我编写的dataweave(我粘贴在下面的一个)未获得所需的输出。

可以请你帮忙!

样本有效载荷

<?xml version="1.0" encoding="UTF-8"?>
<GSBResponse xmlns="http://sampleorg.org/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <GSBResult xmlns:b="http://schemas.org/2004/07/DummyServ" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <b:Reply>0</b:Reply>
        <b:UID>3001</b:UID>
        <b:Counts>
            <b:GBCContent>
                <b:SEQ_NO>1</b:SEQ_NO>
                <b:BNO>112233-444</b:BNO>
                <b:BTYPE>A</b:BTYPE>
                <b:DC>2018-10-08T16:31:36.0754167+02:00</b:DC>
                <b:Values>
                    <b:GBCV>
                        <b:Code>000379</b:Code>
                        <b:Count>2</b:Count>
                        <b:Amount>100</b:Amount>
                    </b:GBCV>
                </b:Values>
                <b:TNO>8934</b:TNO>
                <b:RNO>3334</b:RNO>
            </b:GBCContent>
            <b:GBCContent>
                <b:SEQ_NO>8</b:SEQ_NO>
                <b:BNO>112233-555</b:BNO>
                <b:BTYPE>S</b:BTYPE>
                <b:DC>2018-10-08T16:31:36.0754167+02:00</b:DC>
                <b:Values>
                    <b:GBCV>
                        <b:Code>000379</b:Code>
                        <b:Count>2</b:Count>
                        <b:Amount>100</b:Amount>
                    </b:GBCV>
                    <b:GBCV>
                        <b:Code>000400</b:Code>
                        <b:Count>3</b:Count>
                        <b:Amount>200</b:Amount>
                    </b:GBCV>
                </b:Values>
                <b:TNO>8934</b:TNO>
                <b:RNO>3334</b:RNO>
            </b:GBCContent>
        </b:Counts>
    </GSBResult>
</GSBResponse>

我得到的输出:

[
  {
    "SEQ_NO": "1",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-444",
    "BTYPE": "A",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100"
  },
  {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100",
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000400",
    "Count": "3",
    "Amount": "200"
  }
]

预期产量

[
  {
    "SEQ_NO": "1",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-444",
    "BTYPE": "A",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100"
  },
  {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000379",
    "Count": "2",
    "Amount": "100",
               },
               {
    "SEQ_NO": "8",
    "STORE_NUMBER": "3001",
    "DC": "2018-10-08T16:31:36.0754167+02:00",
    "BNO": "112233-555",
    "BTYPE": "S",
    "COUNT_NUMBER": "8934",
    "REGISTRATION_NUMBER": "3334",
    "Code": "000400",
    "Count": "3",
    "Amount": "200"
  }
]

数据编织

        %dw 1.0
        %output application/java
        %namespace ns0 http://schemas.org/2004/07/DummyServ
        %namespace ns01 http://sampleorg.org/
        ---
        (payload.ns01#GSBResponse.ns01#GSBResult.ns0#Counts.*ns0#GBCContent map ((payload01, indexofPayload01) -> {
        (payload01.ns0#Values.*ns0#GBCV map ((payload02,indexofPayload02) -> {
        SEQ_NO: payload01.ns0#SEQ_NO,
                   DC: payload01.ns0#DC,
                   BNO: payload01.ns0#BNO,
                   BTYPE: payload01.ns0#BTYPE,
                   TNO: payload01.ns0#TNO,
                   RNO: payload01.ns0#RNO,
                   Code: payload02.ns0#Code,
                   Count: payload02.ns0#Count,
                   Amount: payload02.ns0#Amount
            }))
        }))

1 个答案:

答案 0 :(得分:0)

这是一种可能的解决方案:

%dw 1.0
%output application/json
%namespace b http://schemas.org/2004/07/DummyServ

%var content = payload.GSBResponse.GSBResult.b#Counts.*b#GBCContent
%var storeNumber = payload.GSBResponse.GSBResult.b#UID
---
content map using (seqNo = $.b#SEQ_NO, bno = $.b#BNO, btype = $.b#BTYPE, dc = $.b#DC, tno = $.b#TNO, rno = $.b#RNO) (
    ($.Values.*GBCV map {
        SEQ_NO: seqNo,
        DC: dc,
        BNO: bno,
        BTYPE: btype,
        TNO: tno,
        RNO: rno,
        Code: $.b#Code,
        Count: $.b#Count,
        Amount: $.b#Amount
    })
) reduce ($$ ++ $)

这是最大的SEQ_NO:

%dw 1.0
%output application/java
%namespace b http://schemas.org/2004/07/DummyServ

---
max payload.GSBResponse.GSBResult.b#Counts.*b#GBCContent.b#SEQ_NO