Как получить все узлы XML по имени?

У меня есть вводимый 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>

person Rahul Sevani    schedule 15.05.2019    source источник


Ответы (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>

введите описание изображения здесь

person Ryan Carter    schedule 15.05.2019