如何在 Mule Data weave 2.0 中进行 XML 转换
How to do XML transformation in Mule Data weave 2.0
我有两个来自数据库的结果集,一个在有效负载中,另一个在另一个变量中。我正在尝试遍历多个结果集以形成 xml。在第二次迭代期间,我需要从第二个结果集中获取多个实体标签。
有效负载:
[
{
"Version": "1.0",
"ID": "VKP",
"Password": "VKP",
"Username": "VKP",
"id": "123456789",
"Amount": "1000",
"StreetAddress": "Oaks Ave"
}
]
vars.borrowerResult
:
[
{
"firstname": "Vinoy",
"lastname" :"VKP"
},
{
"firstname": "Kevin",
"lastname" :"Peter"
}
]
当前代码:
%dw 2.0
output application/xml
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
//Below is the place i am trying to add the code to iterate over variabe.
// If there are multiple objects i need to get as much entries are there in Array.
vars.borrowerResult map{
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
}
}
}
预期XML输出是
<?xml version='1.0' encoding='UTF-8'?>
<DRIVERequest version="1.00">
<Authentication ID="*****" Password="******" Username="******"/>
<BatchRequest>
<Mortgage id="123456789" Amount="1000">
<Property StreetAddress="Oaks Ave"/>
<Borrower firstname="Vinoy" lastname="VKP"/>
<Borrower firstname="Kevin" lastname="Peter"/>
</Mortgage>
</BatchRequest>
</DRIVERequest>
试试这个:
%dw 2.0
output application/xml
//2nd Result set in Variable
var borrowerResult= [
{
"firstname": "Vinoy",
"lastname" :"VKP"
},
{
"firstname": "Kevin",
"lastname" :"Peter"
}
]
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
(borrowerResult map{
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
})
}
}
}
编辑:啊.. Salim 发布了相同的答案。应该刷新页面 :) 我正忙着清理格式嘿嘿。
output application/xml
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
(vars.borrowerResult map {
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
})
}
}
}
地图周围的括号是必要的,因为它们基本上代表“将其扩展到父对象中”。没有它们,您将需要为要分配给的数组设置一个键。
我有两个来自数据库的结果集,一个在有效负载中,另一个在另一个变量中。我正在尝试遍历多个结果集以形成 xml。在第二次迭代期间,我需要从第二个结果集中获取多个实体标签。
有效负载:
[
{
"Version": "1.0",
"ID": "VKP",
"Password": "VKP",
"Username": "VKP",
"id": "123456789",
"Amount": "1000",
"StreetAddress": "Oaks Ave"
}
]
vars.borrowerResult
:
[
{
"firstname": "Vinoy",
"lastname" :"VKP"
},
{
"firstname": "Kevin",
"lastname" :"Peter"
}
]
当前代码:
%dw 2.0
output application/xml
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
//Below is the place i am trying to add the code to iterate over variabe.
// If there are multiple objects i need to get as much entries are there in Array.
vars.borrowerResult map{
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
}
}
}
预期XML输出是
<?xml version='1.0' encoding='UTF-8'?>
<DRIVERequest version="1.00">
<Authentication ID="*****" Password="******" Username="******"/>
<BatchRequest>
<Mortgage id="123456789" Amount="1000">
<Property StreetAddress="Oaks Ave"/>
<Borrower firstname="Vinoy" lastname="VKP"/>
<Borrower firstname="Kevin" lastname="Peter"/>
</Mortgage>
</BatchRequest>
</DRIVERequest>
试试这个:
%dw 2.0
output application/xml
//2nd Result set in Variable
var borrowerResult= [
{
"firstname": "Vinoy",
"lastname" :"VKP"
},
{
"firstname": "Kevin",
"lastname" :"Peter"
}
]
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
(borrowerResult map{
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
})
}
}
}
编辑:啊.. Salim 发布了相同的答案。应该刷新页面 :) 我正忙着清理格式嘿嘿。
output application/xml
fun StreetAddressSizeCheck(data) = if(data != null and sizeOf(data) > 30) data[0 to 29] else data
---
DRIVERequest @(version : "1.00") : {
Authentication @(ID : "*****", Password : "******", Username : "******") : null,
BatchRequest : payload map {
Mortgage @(id : $.id, Amount : $.Amount) : {
Property @(StreetAddress : StreetAddressSizeCheck($.StreetAddress)) : null,
(vars.borrowerResult map {
Borrower @(firstname :$.firstname ,lastname : $.lastname) : null,
})
}
}
}
地图周围的括号是必要的,因为它们基本上代表“将其扩展到父对象中”。没有它们,您将需要为要分配给的数组设置一个键。