组合来自 2 个不同对象的属性 - datawave

发布于 2025-01-13 05:08:59 字数 500 浏览 3 评论 0原文

object:

<Result>
  <Test>
    <Value>First</Value>
  </Test>
</Result>

object2:

<Result>
  <Test>
    <Value>Second</Value>
  </Test>
</Result>

2个对象的组合,我们可以使用第一个对象,只需添加object2的属性即可。 所需的输出应如下所示:

<Result>
  <Test>
    <Value>First</Value>
    <Value>Second</Value>
  </Test>
</Result>

object:

<Result>
  <Test>
    <Value>First</Value>
  </Test>
</Result>

object2:

<Result>
  <Test>
    <Value>Second</Value>
  </Test>
</Result>

Combination of 2 objects, We can use the first object and just add the attribute of the object2 to it.
The Desired output should look like this:

<Result>
  <Test>
    <Value>First</Value>
    <Value>Second</Value>
  </Test>
</Result>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苹果你个爱泡泡 2025-01-20 05:08:59

这些并不是 DataWeave 中真正的对象。它们是包含 XML 文档的字符串。它们可能位于具有正确内容类型的有效负载和变量中,DataWeave 会自动解析为对象。为了简化示例,我将在脚本中手动解析它们。

在此解决方案中,我将从两个对象(在本例中为键值集合)获取 Result.Test 的值,并将它们连接在一起。

%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>

Those are not really objects in DataWeave. They are strings that contain an XML document. They may be in the payload and variables with the correct content type and DataWeave will parse automatically into objects. To simplify the example I will parse them manually in my script.

In this solution I will take the value of Result.Test from both objects, in this case a collection of key-values, and concatenate them together.

%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
    }
}

Output:

<?xml version='1.0' encoding='UTF-8'?>
<Result>
  <Test>
    <Value>First</Value>
    <Value>Second</Value>
  </Test>
</Result>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文