我已经输入了这样的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>
答案 0 :(得分: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>