-->

How to apply predicate to GPath when I am using XM

2019-08-30 03:15发布

问题:

lets say I have the following XML

<InvoiceLines>
            <InvoiceLine>
                <InsertionDate>29.01.2015</InsertionDate>
                <Productnbr>0102</Productnbr>
            </InvoiceLine>
            <InvoiceLine>
                <InsertionDate>29.02.2015</InsertionDate>
                <Productnbr>0103</Productnbr>
            </InvoiceLine>
</InvoiceLines>

and I would like to get the insertion date of InvoiceLine that has Productnbr = 0103. If I would write xpath I would write somthing like:

//InvoiceLine[./Productnbr='0103']/InsertionDate

but I would like to use GPath since I am using XMLSlurper in my code. Is there a way how to apply predicate to GPath? Thank you

回答1:

You can use a tree search with find and then get the value of it's child:

def date = new XmlSlurper().parseText(xml)
                           .'**'
                           .find { it.Productnbr == '0103' }?.InsertionDate