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?
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
it.@AllowChild
to refer to a field (not it.@AllowChild()
)*.@Num
to extract Num
fields to a list (not .@Num
)Following code:
List<String> childSeatNos = response.extract()
.xmlPath()
.getList("response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num");
produces list:
[1E, 1F]