【问题标题】:How to get all XML nodes by name?如何按名称获取所有 XML 节点?
【发布时间】:2019-10-02 10:00:41
【问题描述】:

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

【问题讨论】:

    标签: mule anypoint-studio dataweave mule4


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多