regexxpathrest-assuredgpath

Finding a partial String match in an XmlPath expression using grep


I'm using RestAssured to help me with some testing.

Given the following 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"/>

How can I pick out all meals that contain the word 'child' in the Description attribute? I need it to be case insensitive.

The following throws no exceptions, but neither does it find the Code attribute 'CHML' I need:

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

I guess my Regex/grep is wrong?


Solution

  • I've managed to get results with your XML using this XPath query:

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

    I've modified the input a bit - inserted 'foo' as a root node and added a second result.

    The modified input 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>
    

    Here's how it looks like. The return type used here is "Nodeset", otherwise not all results are returned.

    enter image description here

    You can test it at http://www.utilities-online.info/xpath/?save=55e705ac-14be-4d75-8fda-507c8da69e2d-xpath