使用grep在XmlPath表达式中查找部分字符串匹配

时间:2018-06-10 10:25:56

标签: regex xpath rest-assured gpath

我使用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属性中?我需要它不区分大小写。

以下内容没有例外,但它也没有找到Code属性&#39; CHML&#39;我需要:

List<String> allChildMeals;
            allChildMeals = response.xmlPath().getList("FAB_BasketRS.CurrentBasket.Itinerary.ItineraryOptions.OptionalExtra.findAll{it.@Type=='Meal' && it.@Description.grep(/[Child]/)}*.@Code");

我猜我的Regex / grep错了?

1 个答案:

答案 0 :(得分:1)

我已设法使用此XPath查询获取XML结果:

/foo/OptionalExtra[@Type='Meal' and contains(@Description, 'Child')]/@Code

我已经对输入进行了一些修改 - 插入&#39; foo&#39;作为根节点并添加第二个结果。

修改后的输入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>

这是它的样子。这里使用的返回类型是&#34; Nodeset&#34;,否则不会返回所有结果。

enter image description here

您可以在以下位置进行测试 http://www.utilities-online.info/xpath/?save=55e705ac-14be-4d75-8fda-507c8da69e2d-xpath