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

How to get all XML nodes by name?

我是这样输入的 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>

并且我想通过 name.For 示例聚合所有 grandChild 节点,如果我想拉 payload.parent.child.*grandChild filter($.@name == 'aaa') 我应该得到包含 3 个 grandChild 节点的数组列表。有什么办法可以做到这一点?

感谢您的帮助。

输出-

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

这 returns 您使用 ..* 选择器检索所有子项并重建输出结构所需的输出:

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