1

XMLがあるとします:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <level0 id="1" t="0">
      <level1 id="lev1id01" att1="2015-05-12" val="12" status="0"/>
      <level1 id="lev1id02" att1="2015-06-13" val="13" status="0"/>
      <level1 id="lev1id03" att1="2015-07-10" val="13" status="0"/>
    </level0>

    <level0 id="2" t="0">
        <level1 id="lev1id11" att1="2015-05-12" val="121" status="0"/>
        <level1 id="lev1id12" att1="2015-06-13" val="132" status="0"/>
        <level1 id="lev1id13" att1="2015-07-11" val="113" status="0"/>
    </level0>

    <level0 id="2" t="1">
        <level1 id="lev1id21" att1="2015-05-12" val="121" status="0"/>
        <level1 id="lev1id22" att1="2015-06-13" val="132" status="0"/>
        <level1 id="lev1id23" att1="2015-07-11" val="113" status="0"/>
        <level1 id="lev1id23" att1="2015-07-11" val="113" status="0"/>
    </level0>
</data>

level0次のすべてのノードを(GPathを使用して)取得したい:

  1. 次に、このlevel0/@t="0"ノード ( level0 ) を選択するのは、すべてlevel1子が@status="0"
  2. level0/@t!="0"最後が. _ _ 最後に言うときは、最大値を持つノードを意味します (形式に日付が含まれていると仮定します)。 level1@status="0"level1@att1@att1yyyy-mm-dd

XPathではmax() や count() などの関数を使用しますが、GPathを使用してそれを行う方法がわかりません。

ありがとう

4

1 に答える 1

2

Groovy で定義されている and 関数は、GPathmax()式内で XPath の同等の代わりに使用できます。count()Iterable

// This closure is for level0[t=0] elements.
// It selects the level0 if the count of its level1[status=0] children is 0.
def t0Select = { level0 -> 
    level0.level1.count { level1 -> level1.@status != '0' } == 0 
}

// This closure is for level1[t=1] elements.
// It selects the level0 if its level1 element with the maximum date has a status of "0" 
def t1Select = { level0 -> 
    level0.level1.max { level1 -> Date.parse('yyyy-MM-dd', level1.@att1.toString()) }?.@status == '0' 
}

// Parse the XML and delegate to the appropriate closure above as per the t attribute
def selected = new XmlSlurper().parseText(xml).level0.findAll { level0 -> 
    level0.@t == '0' ? t0Select(level0) : t1Select(level0) 
}
于 2016-08-09T12:21:40.130 に答える