【问题标题】:how to split all the xml tags in Mule 4?如何拆分 Mule 4 中的所有 xml 标签?
【发布时间】:2019-05-10 14:48:55
【问题描述】:

我有一个这样的输入 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 上的一些收集分离器替代品或任何其他可能的方式。

【问题讨论】:

    标签: mule anypoint-studio dataweave mule4


    【解决方案1】:

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

    【讨论】:

    • 您好,感谢您的回复。您是否尝试运行代码,它在 mapObject 处给了我错误Cannot coerce Array ([{Attribute @(name: "xxx"): "1",grandChild @(name: "bbb", action: "None"): {A...) to Object,因为 mapObject 需要一个对象,但我们有一个数组。我试过投射它,但没有用。
    • 我在 Mule 4.1.3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多