结合来自 2 个不同对象的属性 - datawave
Combine attributes from 2 different objects - datawave
对象:
<Result>
<Test>
<Value>First</Value>
</Test>
</Result>
对象 2:
<Result>
<Test>
<Value>Second</Value>
</Test>
</Result>
2个对象的组合,我们可以使用第一个对象,只需将object2的属性添加到它。
所需的输出应如下所示:
<Result>
<Test>
<Value>First</Value>
<Value>Second</Value>
</Test>
</Result>
那些在 DataWeave 中并不是真正的对象。它们是包含 XML 文档的字符串。它们可能位于具有正确内容类型的负载和变量中,DataWeave 将自动解析为对象。为了简化示例,我将在我的脚本中手动解析它们。
在这个解决方案中,我将从两个对象中获取 Result.Test 的值,在本例中是 key-values 的集合,并将它们连接在一起。
%dw 2.0
output application/xml
import mergeWith from dw::core::Objects
var in1=read("<Result>
<Test>
<Value>First</Value>
</Test>
</Result>", "application/xml")
var in2=read("<Result>
<Test>
<Value>Second</Value>
</Test>
</Result>", "application/xml")
---
{
Result: {
Test: in1.Result.Test ++ in2.Result.Test
}
}
输出:
<?xml version='1.0' encoding='UTF-8'?>
<Result>
<Test>
<Value>First</Value>
<Value>Second</Value>
</Test>
</Result>
对象:
<Result>
<Test>
<Value>First</Value>
</Test>
</Result>
对象 2:
<Result>
<Test>
<Value>Second</Value>
</Test>
</Result>
2个对象的组合,我们可以使用第一个对象,只需将object2的属性添加到它。 所需的输出应如下所示:
<Result>
<Test>
<Value>First</Value>
<Value>Second</Value>
</Test>
</Result>
那些在 DataWeave 中并不是真正的对象。它们是包含 XML 文档的字符串。它们可能位于具有正确内容类型的负载和变量中,DataWeave 将自动解析为对象。为了简化示例,我将在我的脚本中手动解析它们。
在这个解决方案中,我将从两个对象中获取 Result.Test 的值,在本例中是 key-values 的集合,并将它们连接在一起。
%dw 2.0
output application/xml
import mergeWith from dw::core::Objects
var in1=read("<Result>
<Test>
<Value>First</Value>
</Test>
</Result>", "application/xml")
var in2=read("<Result>
<Test>
<Value>Second</Value>
</Test>
</Result>", "application/xml")
---
{
Result: {
Test: in1.Result.Test ++ in2.Result.Test
}
}
输出:
<?xml version='1.0' encoding='UTF-8'?>
<Result>
<Test>
<Value>First</Value>
<Value>Second</Value>
</Test>
</Result>