mule - 如何拆分 Mule 4 中的所有 xml 标签?
问题描述
我有一个这样的输入xml
<parent>
<child type="reference">
<grandChild name="aaa" action="None">
<Attribute name="xxx">1</Attribute>
<grandChild name="bbb" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
<grandChild name="ccc" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</grandChild>
<grandChild name="ddd" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
我想将它拆分为只有 parent->child->grandChild 标签的多个 xml,总共应该将上面的示例转换为 4 个 xml(因为有 4 个 grandChild)。像这样 -
<parent>
<child type="reference">
<grandChild name="aaa" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
<parent>
<child type="reference">
<grandChild name="bbb" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
<parent>
<child type="reference">
<grandChild name="ccc" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
<parent>
<child type="reference">
<grandChild name="ddd" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
有人可以指导我吗?我一直在寻找 Mule 3 上的一些收集分离器替代品或任何其他可能的方式。
解决方案
这将创建一个包含这些 xml 的数组,因此如果您将此表达式放在 foreach 表达式中,它将执行您需要的操作。
%dw 2.0
import dw::core::Objects
fun collectChilds(node) = do {
var children = node.&grandChild default {}
---
(children mapObject ((item, key) -> {
parent: {
child @("type": "reference") : {
(key) : item.&Attribute
}
}
}) pluck ((value, key, index) -> {(key) : value})
) ++ (Objects::valueSet(children) flatMap ((item, index) -> collectChilds(item)))
}
---
collectChilds(payload.parent.child)
推荐阅读
- macos - 禁止 dyld 加载消息
- swift - 是否有一个真正的错误方法来查看当前的 SKView 是什么?
- kotlin - kotlin REPL 是否具有代码完成功能?
- ios - 如何使用 SwiftyJSON 读取数组中的 JSON 数据?
- c++ - 将 Linux 库项目转换为可在 Windows 中使用的库项目
- angular - 角占位符文本正在隐藏在行下
- mikro-orm - ST_Distance_Sphere 使用 MySQL 驱动程序的 Mikro-orm 命令
- cassandra - 指定分区键时对 cassandra 中的二级索引进行查询
- elasticsearch - 检查数组是否包含字段 == x 的对象
- java - Selenium 远程独立服务器 - 驱动程序未知