在 mule 4 中使用带有 XML 数据的地图运算符时无法强制数组错误?
Cannot coerce Array error while using map operator with XML data in mule 4?
我在使用地图运算符时遇到以下错误:
org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
description="Cannot coerce Array (org.mule.weave.v2.model.values.ArrayValue$IteratorArrayValue@22af825a) to String
Trace:
at main (Unknown), while writing Xml
有效负载:
%dw 2.0
output application/xml
ns cc someUrl
---
(vars.products*product map {
cc #productDetails: {
cc #productCategory: $.productCategory,
cc #productName: $.productName,
cc #productImageData: $.productImageData
}
})
产品:
[
product:{productCategory= "A", productName="name", productImageData=base64 string},
product:{productCategory= "B", productName="name2", productImageData=base64 string},
product:{productCategory= "C", productName="name3", productImageData=base64 string}
]
XML 中没有数组。我通过使用 reduce() 连接数组中的对象来解决这个问题。我还添加了一个根元素,它在 XML.
中是必需的
为简单起见,我只是将产品作为变量添加到脚本中:
%dw 2.0
output application/xml
ns cc someUrl
var products=[
product:{productCategory: "A", productName:"name", productImageData:"base64 string"},
product:{productCategory: "B", productName:"name2", productImageData:"base64 string"},
product:{productCategory: "C", productName:"name3", productImageData:"base64 string"}
]
---
result: ( products.*product map {
cc #productDetails: {
cc #productCategory: $.productCategory,
cc #productName: $.productName,
cc #productImageData: $.productImageData
}
} ) reduce ((item, accumulator={}) -> item ++ accumulator )
输出:
<?xml version='1.0' encoding='UTF-8'?>
<result>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>C</cc:productCategory>
<cc:productName>name3</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>B</cc:productCategory>
<cc:productName>name2</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>A</cc:productCategory>
<cc:productName>name</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
</result>
我在使用地图运算符时遇到以下错误:
org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
description="Cannot coerce Array (org.mule.weave.v2.model.values.ArrayValue$IteratorArrayValue@22af825a) to String
Trace:
at main (Unknown), while writing Xml
有效负载:
%dw 2.0
output application/xml
ns cc someUrl
---
(vars.products*product map {
cc #productDetails: {
cc #productCategory: $.productCategory,
cc #productName: $.productName,
cc #productImageData: $.productImageData
}
})
产品:
[
product:{productCategory= "A", productName="name", productImageData=base64 string},
product:{productCategory= "B", productName="name2", productImageData=base64 string},
product:{productCategory= "C", productName="name3", productImageData=base64 string}
]
XML 中没有数组。我通过使用 reduce() 连接数组中的对象来解决这个问题。我还添加了一个根元素,它在 XML.
中是必需的为简单起见,我只是将产品作为变量添加到脚本中:
%dw 2.0
output application/xml
ns cc someUrl
var products=[
product:{productCategory: "A", productName:"name", productImageData:"base64 string"},
product:{productCategory: "B", productName:"name2", productImageData:"base64 string"},
product:{productCategory: "C", productName:"name3", productImageData:"base64 string"}
]
---
result: ( products.*product map {
cc #productDetails: {
cc #productCategory: $.productCategory,
cc #productName: $.productName,
cc #productImageData: $.productImageData
}
} ) reduce ((item, accumulator={}) -> item ++ accumulator )
输出:
<?xml version='1.0' encoding='UTF-8'?>
<result>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>C</cc:productCategory>
<cc:productName>name3</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>B</cc:productCategory>
<cc:productName>name2</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
<cc:productDetails xmlns:cc="someUrl">
<cc:productCategory>A</cc:productCategory>
<cc:productName>name</cc:productName>
<cc:productImageData>base64 string</cc:productImageData>
</cc:productDetails>
</result>