在GPath groovy语句中使用AND子句来获取所有匹配大于1的条件的xml节点
问题描述:
我是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.
什么是正确的语法?
答
使用&&
逻辑AND
运算符,而不是单个&
这是一个按位“和”运算符。同时改变你的表达:
response."**".findAll { it.name() == 'Seat' && [email protected] == 'true'}*[email protected]
- 使用
[email protected]
指一个字段(不[email protected]()
) - 使用蔓延运营商
*[email protected]
提取Num
字段列表(不[email protected]
)
以下代码:
List<String> childSeatNos = response.extract()
.xmlPath()
.getList("response."**".findAll { it.name() == 'Seat' && [email protected] == 'true'}*[email protected]");
产生列表:
[1E, 1F]
谢谢,这很有道理。不过,我仍然收到一个IllegalArgumentException,所以我猜在语句中仍然存在一些语法错误 – Steerpike
@Steerpike我在你的表达式中发现了两个更多的语法错误,回答更新 –
太棒了!谢谢 – Steerpike