首页 > 解决方案 > 如何拆分 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 上的一些收集分离器替代品或任何其他可能的方式。

标签: muleanypoint-studiodataweavemule4

解决方案


这将创建一个包含这些 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)

推荐阅读