在GPath groovy语句中使用AND子句以使所有xml节点匹配> 1条件

时间:2017-10-17 08:51:18

标签: xml xpath groovy gpath

我是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.

正确的语法是什么?

1 个答案:

答案 0 :(得分:0)

&&用于逻辑AND运算符,而不是单个&,这是一个按位“和”运算符。同时将表达式更改为:

response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num
  • 使用it.@AllowChild来引用字段(不是it.@AllowChild()
  • 使用点差运算符*.@NumNum字段提取到列表(不是.@Num

以下代码:

List<String> childSeatNos = response.extract()
        .xmlPath()
        .getList("response."**".findAll { it.name() == 'Seat' && it.@AllowChild == 'true'}*.@Num");

生成列表:

[1E, 1F]
相关问题