2

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

我想拉所有按名称聚合的 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>
4

1 回答 1

4

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

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

在此处输入图像描述

于 2019-05-15T12:01:03.380 回答