如何在 DataWeave 1.0 中将数组转换为 XML 个对象?
How to transform array to XML objects in DataWeave 1.0?
如何将以下对象转换为 XML?
%dw 1.0
%output application/xml encoding="UTF-8", skipNullOn="everywhere"
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
---
soap#Body: {
Array: [
{
Item:"test 1"
},
{
Item:"test 2"
}
]
}
预期输出与 DataWeave 2.0 相同
<?xml version='1.0' encoding='UTF-8'?>
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<Array>
<Item>test 1</Item>
</Array>
<Array>
<Item>test 2</Item>
</Array>
</soap:Body>
但目前我得到了一个错误
Message : Exception while executing:
Array: [
^
Cannot coerce a :array to a :object.
那是因为DataWeave可以将Objects转换成XML个元素,但是XML中没有数组的概念,所以失败了。相反,您需要将数组转换为对象。我通过连接每个项目对象来使用 reduce() 函数。请注意,您错误地使用了 Array,因为您想将每个项目包含在一个单独的 Array 元素中。
%dw 1.0
%output application/xml encoding="UTF-8", skipNullOn="everywhere"
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
---
{
soap#Body:
[
{
Item:"test 1"
},
{
Item:"test 2"
}
] reduce ((item, accumulator={}) -> accumulator ++ { Array: item})
}
如何将以下对象转换为 XML?
%dw 1.0
%output application/xml encoding="UTF-8", skipNullOn="everywhere"
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
---
soap#Body: {
Array: [
{
Item:"test 1"
},
{
Item:"test 2"
}
]
}
预期输出与 DataWeave 2.0 相同
<?xml version='1.0' encoding='UTF-8'?>
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<Array>
<Item>test 1</Item>
</Array>
<Array>
<Item>test 2</Item>
</Array>
</soap:Body>
但目前我得到了一个错误
Message : Exception while executing:
Array: [
^
Cannot coerce a :array to a :object.
那是因为DataWeave可以将Objects转换成XML个元素,但是XML中没有数组的概念,所以失败了。相反,您需要将数组转换为对象。我通过连接每个项目对象来使用 reduce() 函数。请注意,您错误地使用了 Array,因为您想将每个项目包含在一个单独的 Array 元素中。
%dw 1.0
%output application/xml encoding="UTF-8", skipNullOn="everywhere"
%namespace soap http://schemas.xmlsoap.org/soap/envelope/
---
{
soap#Body:
[
{
Item:"test 1"
},
{
Item:"test 2"
}
] reduce ((item, accumulator={}) -> accumulator ++ { Array: item})
}