mule - 如何按名称获取所有 XML 节点?

标签 mule anypoint-studio dataweave mule4

我有这样的输入 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="aaa" action="None">
                <Attribute name="xxx">2</Attribute>
            </grandChild>
        </grandChild>
        <grandChild name="ddd" action="None">
                <Attribute name="xxx">1</Attribute>
                <grandChild name="aaa" action="None">
                    <Attribute name="xxx">3</Attribute>
                </grandChild>
        </grandChild>
    </child>
</parent>

并且我想拉出所有按名称聚合的孙子节点。例如,如果我想拉出 payload.parent.child.*grandChild filter($.@name == 'aaa') 我应该得到包含 3 个孙节点的数组列表。有什么办法可以实现这一点吗?

感谢您的帮助。

输出 -

<grandChilds>
    <grandChild name="aaa" action="None">
        <Attribute name="xxx">1</Attribute>
    </grandChild>
    <grandChild name="aaa" action="None">
        <Attribute name="xxx">2</Attribute>
    </grandChild>
    <grandChild name="aaa" action="None">
        <Attribute name="xxx">3</Attribute>
    </grandChild>
</grandChilds>

最佳答案

这将使用 ..* 选择器返回所需的输出,以检索所有子项并重建输出结构:

%dw 2.0
output application/xml
---

grandChilds:{
    ( payload.parent..*grandChild filter($.@name == 'aaa') map(gc) ->{

    grandChild @(name: gc.@name, action: gc.@action): {
        Attribute @(name: gc.Attribute.@name): gc.Attribute
    }

})

}

输出:

<?xml version='1.0' encoding='UTF-8'?>
<grandChilds>
  <grandChild name="aaa" action="None">
    <Attribute name="xxx">1</Attribute>
  </grandChild>
  <grandChild name="aaa" action="None">
    <Attribute name="xxx">2</Attribute>
  </grandChild>
  <grandChild name="aaa" action="None">
    <Attribute name="xxx">3</Attribute>
  </grandChild>
</grandChilds>

enter image description here

关于mule - 如何按名称获取所有 XML 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146281/

相关文章:

Mule 4 attribute.maskedRequestPath 预期行为

maven - 未知包 : mule-module error after running mvn archetype for cloud connector creation via DevKit

error-handling - 如何处理 "Couldn' t 在骡子中得到任何响应?

dataweave - 我们可以用 Java 运行 Mulesoft Dataweave 代码吗?

mule - Anypoint Studio-调试器未启动

json - 如何将 ManagedCursorStreamProvider 转换为 mule 4 中的 JSOn 对象

java - Mule/Groovy - Canonicals 列表拆分为另外 3 个列表

proxy - 骡子图案:web-service-proxy and pattern:http-proxy有什么区别

Mule 将数据库导出到 csv 文件

java - 如何使用 Dataweave 删除括号或忽略来自 Scatter-Gather 的括号?