xmlxpathgroovygpath

Using AND clause in a GPath groovy statement to get all xml nodes matching >1 criteria


I am a newcomer to Groovy / GPath and am using it with RestAssured. I need some help with syntax for a query.

Given the following xml snippet:

<?xml version="1.0" encoding="UTF-8"?>
<SeatOptions FlightNumber="GST4747" AircraftType="737" NumberOfBlocks="2" Currency="GBP" Supplier="ABC">
  <Seat Num="1A" Availabile="true" BandId="1" Block="1" Row="1" AllowChild="false" />
  <Seat Num="1B" Availabile="true" BandId="1" Block="1" Row="1" AllowChild="false" />
  <Seat Num="1C" Availabile="true" BandId="1" Block="1" Row="1" AllowChild="false"/>
  <Seat Num="1D" Availabile="true" BandId="1" Block="2" Row="1" AllowChild="false" />
  <Seat Num="1E" Availabile="true" BandId="1" Block="2" Row="1" AllowChild="true" />
  <Seat Num="1F" Availabile="true" BandId="1" Block="2" Row="1" AllowChild="true" />
</SeatOptions>

I can extract all seat numbers as follows:

List<String> allSeatNos = response.extract().xmlPath().getList("**.findAll { it.name() == 'Seat'}.@Num");

How can I extract all seat numbers where AllowChild="true" ?

I have tried:

List<String> childSeatNos = response.extract().xmlPath().getList("**.findAll { it.name() == 'Seat' & it.@AllowChild() == 'true'}.@Num");

It throws:

java.lang.IllegalArgumentException: Path '**'.findAll { it.name() == 'Seat' & it.@AllowChild() == 'true'}.'@Num' is invalid.

What is the correct syntax?


Solution

  • Use && for logical AND operator, not single & which is a bitwise "and" operator. Also change your expression to:

    response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num
    

    Following code:

    List<String> childSeatNos = response.extract()
            .xmlPath()
            .getList("response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num");
    

    produces list:

    [1E, 1F]