0

Given this XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <level0 id="1" t="0" l0id="0">
      <level1 id="lev1id01" att1="2015-05-12" val="12" status="0" year="2015" month="05" />
      <level1 id="lev1id02" att1="2015-06-13" val="13" status="0" year="2015" month="07" />
      <level1 id="lev1id03" att1="2015-07-10" val="13" status="0" year="2015" month="04" />
    </level0>
    <level0 id="2" t="1" l0id="2">
        <level1 id="lev1id21" att1="2015-05-12" val="121" status="0" year="2015" month="05" />
        <level1 id="lev1id22" att1="2015-06-13" val="132" status="0" year="2015" month="06" />
        <level1 id="lev1id23" att1="2015-07-11" val="113" status="0" year="2015" month="08" />
        <level1 id="lev1id24" att1="2015-07-11" val="114" status="0" year="2015" month="07" />
    </level0>
</data>

I need to find all level1 nodes (one per each level0 node) sorted by att1, then year and month values.

For this example I expect to get nodes:

  • level1[@id="lev1id03"]

  • level1[@id="lev1id23"] -- because it has month = 08 which is maximum

I suppose I have to use something like this:

new XmlSlurper().parseText(xml).level0.level1.findAll {level1 -> 
    /* some closure */
}

Unfortunately I'm not an Groovy/GPath expert so I'm looking for a Groovish way to do it. Thanks.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
lospejos
  • 1,976
  • 3
  • 19
  • 35
  • Why did you ask the same question three times, instead of clarifying your question? It is still unclear what you are trying to do, because you have not provided enough examples of what you expect the sample data to return. Show some actual code that prints a result, what the output is, and what is expected instead. – thecodesmith_ Aug 14 '16 at 03:51
  • Although my questions seems like the same, they are different actually, IMHO. One question is about getting the attribute value of max or min element (or some element found by certain conditions). Another one is about founding list of elements (child nodes) by certain conditions. They all require writing closures and all related to GPath (IMHO), but they are not the same. I need both closures for finding a value and finding nodes list since I'm not an expert in Groovy collections / GPath – lospejos Aug 15 '16 at 10:31

0 Answers0