代码之家  ›  专栏  ›  技术社区  ›  star

如何将groupBy与orderBy一起用于Mule4中的Array to XMl

  •  0
  • star  · 技术社区  · 5 年前

    groupBy orderBy 具有 clientId 所以它的内部 Lines 在xml中分组并排序到一个根(clientId的重复行)。我被困在如何使用dataweave进行XMl响应。请查找请求和预期响应。

    请求:

     [
        {
         "ClientId": 2,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "30",
         "Quantity": "20"
       },
       {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "56",
         "Quantity": "20"
      },
      {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "70",
         "Quantity": "20"
      }
      
    ]
    

    回应

    <?xml version='1.0' encoding='UTF-8'?>
     <Receipt>
       <client clientId="1">
         <code>string</code>
         <Lines>
           <Warehouse>56</Warehouse>
           <Quantity>20</Quantity>
         </Lines>
         <Lines>
           <Warehouse>70</Warehouse>
           <Quantity>20</Quantity>
         </Lines>
       </client>
       <client  clientId="2">
         <code>string</code>
         <Lines>
           <Warehouse>30</Warehouse>
           <Quantity>20</Quantity>
         </Lines>
       </client>
     </Receipt>
    

    0 回复  |  直到 5 年前
        1
  •  2
  •   George    5 年前

    尝试此操作,注释嵌入到代码中:

    %dw 2.0
    output application/xml
    
    var data =  [
        {
         "ClientId": 2,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "30",
         "Quantity": "20"
       },
       {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "56",
         "Quantity": "20"
      },
      {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "70",
         "Quantity": "20"
      }
    ]
    
    ---
    Receipt: do {
        // Group by the data
        var groupedData = data groupBy $.ClientId
        // Order the client Ids
        var orderedClientIds = groupedData pluck $$ orderBy $ as Number
        ---
        // Iterate over the ordered clientIds and create an object, hence the use of reduce
        orderedClientIds reduce (cId, cIds={}) -> cIds ++ {
            // Add the clientId attribute to the client tag
            client @(clientId: cId ): {
                // Get the code from the first element in the array
                code: groupedData[cId][0].Code,
                // Create the Lines, should you avoid repeating tags at the
                // same level with other tags?  i.e. code and Lines
                // IMHO a best practice XML should have code, a single Lines
                // and nested in Lines, one or more Line tags organizing the
                // data
                Lines: groupedData[cId]  reduce (
                    (l,ls={}) -> ls ++ Lines :{
                        Warehouse: l.Warehouse,
                        Quantity: l.Quantity
                    }   
                )
            }
        }
    }
    

    我还不清楚的是你是否要订购 Warehouse 和/或 Quantity 价值观。我没有在上面的代码,但你可以很容易地做到这一点,只要让我知道,我会修改。

    Transform Message processor,Studio会显示一个错误——这个错误是假阳性,打开预览或者更好的粘贴一个 HTTP Listener

        2
  •  1
  •   maddestroyer7    5 年前

    我讨厌xml。但这是我要做的。希望有帮助。

    %dw 2.0
    output application/json
    var data =  [
        {
         "ClientId": 2,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "30",
         "Quantity": "20"
       },
       {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "56",
         "Quantity": "20"
      },
      {
         "ClientId": 1,
         "Code": "string",
         "ReceivedDate": "2018-10-23",
         "ReceivedTime": "2217",
         "Warehouse": "70",
         "Quantity": "20"
      }
    ]
    
    var clients = (data orderBy $.ClientId groupBy $.ClientId)
    ---
    {
        Receipt: clients mapObject (v,k,i) -> {client @(clientId: k): {
            code: v.Code[0]
        } ++ ((v map (item,ind) -> {
            Lines: {
                Warehouse: item.Warehouse,
                Quantity: item.Quantity
            }
        }) reduce (value, acc={}) -> acc ++ value)
        }
    }
    

    <?xml version='1.0' encoding='UTF-8'?>
    <Receipt>
      <client clientId="1">
        <code>string</code>
        <Lines>
          <Warehouse>56</Warehouse>
          <Quantity>20</Quantity>
        </Lines>
        <Lines>
          <Warehouse>70</Warehouse>
          <Quantity>20</Quantity>
        </Lines>
      </client>
      <client clientId="2">
        <code>string</code>
        <Lines>
          <Warehouse>30</Warehouse>
          <Quantity>20</Quantity>
        </Lines>
      </client>
    </Receipt>
    
    推荐文章