我有这样的输入 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>
关于mule - 如何按名称获取所有 XML 节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146281/