尝试此操作,注释嵌入到代码中:
%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