如何在 Mule4 中使用 groupBy 和 Array to XMl 上的 orderBy
How to use groupBy with orderBy on Array to XMl in Mule4
我将输入作为数组 (json) 需要 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>
如果问题不清楚,请告诉我。提前致谢。
试试这个,注释嵌入代码中:
%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
and/or Quantity
值。我没有在上面的代码中,但你可以很容易地做到,只要告诉我,我会修改。
编辑:如果您将代码复制并粘贴到 Transform Message
处理器中,Studio 将显示错误——此错误是误报,打开预览或更好地粘贴 HTTP Listener
到源并试一试 :)
我讨厌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>
我将输入作为数组 (json) 需要 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>
如果问题不清楚,请告诉我。提前致谢。
试试这个,注释嵌入代码中:
%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
and/or Quantity
值。我没有在上面的代码中,但你可以很容易地做到,只要告诉我,我会修改。
编辑:如果您将代码复制并粘贴到 Transform Message
处理器中,Studio 将显示错误——此错误是误报,打开预览或更好地粘贴 HTTP Listener
到源并试一试 :)
我讨厌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>