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

Regex 使用grep在XmlPath表达式中查找部分字符串匹配,regex,xpath,rest-assured,gpath,Regex,Xpath,Rest Assured,Gpath,我正在使用restasured来帮助我进行一些测试 给定以下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"/>

我正在使用restasured来帮助我进行一些测试

给定以下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"/>

如何在“描述”属性中选择包含“儿童”一词的所有膳食?我需要它不区分大小写

以下内容不会引发异常,但也找不到我需要的代码属性“CHML”:

List<String> allChildMeals;
            allChildMeals = response.xmlPath().getList("FAB_BasketRS.CurrentBasket.Itinerary.ItineraryOptions.OptionalExtra.findAll{it.@Type=='Meal' && it.@Description.grep(/[Child]/)}*.@Code");
列出所有儿童膳食;
AllChildFeeds=response.xmlPath().getList(“FAB_BasketRS.currentball.itineray.itinerayOptions.OptionalExtra.findAll{it.@Type=='feed'&&it.@Description.grep(/[Child]/)}*@Code”);

我猜我的Regex/grep是错的?

我已经使用这个XPath查询成功地获得了XML的结果:

/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>

下面是它的样子。这里使用的返回类型是“Nodeset”,否则不会返回所有结果

你可以在
[Child]
匹配单个
C
h
i
l
d
字符。我想您想要的是
*Child.
,它将匹配包含
子序列的任何文本。可能是@Aaron的重复。谢谢您的提示。你说的很有道理,而且我学得很快……但是你建议的改进正则表达式仍然没有结果。我想这与我使用它的上下文有关——XmlPath javadoc声明“rest assured的XmlPath实现使用Groovy外壳来计算表达式”我不熟悉XmlPath的grep,但我注意到您使用的是JS中常见但Java(或Groovy AFAIK)中不常见的
/regex/
构造;您可能希望尝试不使用斜杠,除非xmlpath的文档提到要使用斜杠。如果您没有运行regex所需的执行环境,我希望会引发错误并显示在日志中,我假设您没有错误,也没有结果?是的,没错-使用斜杠,我没有结果,但没有异常。如果没有它们,我将获得java.lang.IllegalArgumentException:无效路径:意外标记:。@第1行,第132列。al'&&it.@Description'.grep(.*Child.*)XmlPath Javadoc似乎没有对grep方法的引用。我看到的唯一上下文是:谢谢你的“tpx”,非常感谢。要是我知道你在哪里工作,我就给你买杯咖啡!这个xpath查询不区分大小写,但如果它能工作,就很容易进行调整