使用 grep 在 XmlPath 表达式中查找部分字符串匹配
Finding a partial String match in an XmlPath expression using grep
我正在使用 RestAssured 来帮助我进行一些测试。
给出以下 XML:
<OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="CHML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
如何在 Description 属性中找出所有包含单词 'child' 的餐食?我需要它不区分大小写。
下面没有抛出异常,但也没有找到我需要的代码属性'CHML':
List<String> allChildMeals;
allChildMeals = response.xmlPath().getList("FAB_BasketRS.CurrentBasket.Itinerary.ItineraryOptions.OptionalExtra.findAll{it.@Type=='Meal' && it.@Description.grep(/[Child]/)}*.@Code");
我猜我的Regex/grep错了?
我已经设法通过您的 XML 使用此 XPath 查询获得结果:
/foo/OptionalExtra[@Type='Meal' and contains(@Description, 'Child')]/@Code
我稍微修改了输入 - 插入 'foo' 作为根节点并添加了第二个结果。
修改后的输入XML:
<foo>
<OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="FIRST RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="SECOND RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
</foo>
这是它的样子。这里使用的return类型是"Nodeset",否则并不是所有的结果都是returned。
您可以在
http://www.utilities-online.info/xpath/?save=55e705ac-14be-4d75-8fda-507c8da69e2d-xpath
我正在使用 RestAssured 来帮助我进行一些测试。
给出以下 XML:
<OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="CHML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
如何在 Description 属性中找出所有包含单词 'child' 的餐食?我需要它不区分大小写。
下面没有抛出异常,但也没有找到我需要的代码属性'CHML':
List<String> allChildMeals;
allChildMeals = response.xmlPath().getList("FAB_BasketRS.CurrentBasket.Itinerary.ItineraryOptions.OptionalExtra.findAll{it.@Type=='Meal' && it.@Description.grep(/[Child]/)}*.@Code");
我猜我的Regex/grep错了?
我已经设法通过您的 XML 使用此 XPath 查询获得结果:
/foo/OptionalExtra[@Type='Meal' and contains(@Description, 'Child')]/@Code
我稍微修改了输入 - 插入 'foo' 作为根节点并添加了第二个结果。
修改后的输入XML:
<foo>
<OptionalExtra ID="PREB" Description="Premium meal beef" Code="PREB" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="79.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="FIRST RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="VLML" Description="Vegetarian meal" Code="VLML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="GFML" Description="Gluten-free meal" Code="GFML" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
<OptionalExtra ID="CHML" Description="Child meal" Code="SECOND RESULT" Supplier="PRI" FPC="extra.pax.flightmeal" Type="Meal" QuantityAvailable="3" UnitCost="23.98" CurrencyCode="GBP" ApplyTo="SelectedPax"/>
</foo>
这是它的样子。这里使用的return类型是"Nodeset",否则并不是所有的结果都是returned。
您可以在 http://www.utilities-online.info/xpath/?save=55e705ac-14be-4d75-8fda-507c8da69e2d-xpath