2017-10-17 24 views
1

我是Groovy/GPath的新成員,並且正在與RestAssured一起使用它。我需要一些關於查詢語法的幫助。在GPath groovy語句中使用AND子句來獲取所有匹配大於1的條件的xml節點

考慮到下面的XML片段:

<?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> 

我可以提取所有座位號碼如下:

List<String> allSeatNos = response.extract().xmlPath().getList("**.findAll { it.name() == 'Seat'}[email protected]"); 

我怎麼能提取所有座位號,其中AllowChild = 「真」?

我曾嘗試:

List<String> childSeatNos = response.extract().xmlPath().getList("**.findAll { it.name() == 'Seat' & [email protected]() == 'true'}[email protected]"); 

它拋出:

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

什麼是正確的語法?

回答

0

使用&&邏輯AND運算符,而不是單個&這是一個按位「和」運算符。同時改變你的表達:

response."**".findAll { it.name() == 'Seat' && [email protected] == 'true'}*[email protected] 

以下代碼:

List<String> childSeatNos = response.extract() 
     .xmlPath() 
     .getList("response."**".findAll { it.name() == 'Seat' && [email protected] == 'true'}*[email protected]"); 

產生列表:

[1E, 1F] 
+0

謝謝,這很有道理。不過,我仍然收到一個IllegalArgumentException,所以我猜在語句中仍然存在一些語法錯誤 – Steerpike

+0

@Steerpike我在你的表達式中發現了兩個更多的語法錯誤,回答更新 –

+0

太棒了!謝謝 – Steerpike