我是Groovy / GPath的新手,并将其与RestAssured一起使用。我需要一些查询语法方面的帮助。
给出以下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'}.@Num");
如何提取AllowChild =“true”的所有座位号?
我试过了:
List<String> childSeatNos = response.extract().xmlPath().getList("**.findAll { it.name() == 'Seat' & it.@AllowChild() == 'true'}.@Num");
它抛出:
java.lang.IllegalArgumentException: Path '**'.findAll { it.name() == 'Seat' & it.@AllowChild() == 'true'}.'@Num' is invalid.
正确的语法是什么?
答案 0 :(得分:0)
将&&
用于逻辑AND
运算符,而不是单个&
,这是一个按位“和”运算符。同时将表达式更改为:
response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num
it.@AllowChild
来引用字段(不是it.@AllowChild()
)*.@Num
将Num
字段提取到列表(不是.@Num
)以下代码:
List<String> childSeatNos = response.extract()
.xmlPath()
.getList("response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num");
生成列表:
[1E, 1F]