1

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?

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
Steerpike
  • 1,712
  • 6
  • 38
  • 71

1 Answers1

0

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
  • use it.@AllowChild to refer to a field (not it.@AllowChild())
  • use spread operator *.@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]
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • thanks, that makes sense. However I'm still receiving an IllegalArgumentException so I guess there remains some syntactical error somwehere within the statement – Steerpike Oct 17 '17 at 09:14
  • @Steerpike I've found two more syntactic errors in your expression, answer updated – Szymon Stepniak Oct 17 '17 at 09:36