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

Posted

技术标签:

【中文标题】在 DataWeave 中映射嵌套地图时出现问题【英文标题】:Issue while mapping nested map in DataWeave 【发布时间】:2019-03-13 08:58:37 【问题描述】:

我从 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
            ))
        ))

【问题讨论】:

您应该编辑您的问题,以包括您的预期输出与您得到的输出有何不同。仅查看有效载荷很难判断。此外,您应该格式化您的 DW 代码,使其易于理解。到目前为止,它只是一串未格式化的文本。更多信息在这里:meta.stackexchange.com/questions/22186/… 更新了提要 【参考方案1】:

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

%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

【讨论】:

感谢@utechtzs .. 这会破坏数组,但我也想从 xml 流中找到最新的 seq_no,以便我可以将其存储在暂存中以备后用。如何做到这一点?提前致谢! @PiyushSingh 抱歉,答案已更新。如果您需要其他任何东西,请告诉我。谢谢!

以上是关于在 DataWeave 中映射嵌套地图时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

使用地图转换消息时出现 Biztalk 2020 异常

将结构插入地图时出现分段错误

尝试在 React 中映射子对象时出现未定义错误

访问 Javascript 映射中的值时出现问题,函数执行顺序可能存在问题

删除以结构为键的地图时出现分段错误

解析序列/映射节点时出现无效的 yaml 节点错误