Search code examples
xmlxpathexpressioncontains

xpath expression to check multiple values


This is my xpath expression:

countries.xpath=//[local-name()='CountryIdentifier']/text()='1067' or //[local-name()='CountryIdentifier']/text()='874' or //*[local-name()='CountryIdentifier']/text()='994' .............

like i have 50 countries to add in the above expression so it will be quite big xpath expression

Is there any way to use "contains method" or to reduce the size of the expression like

eg: countries.xpath=//*[local-name()='CountryIdentifier']/text()=[1067,874,994,..]


Solution

  • In XPath 2.0 you can write

    //*:CountryIdentifier[.=(1067,874,994,..)]
    

    In 1.0 you can write

    //*[local-name()='CountryIdentifier'][.=1067 or .=874 or .=994 ...]
    

    Try to avoid using text(): you don't want the text nodes, you want the string value of the element. Using text() makes your expression less robust, eg. it fails if there are comments or if the elements you are selecting have internal markup. And in an expression like this, it makes your code much more long-winded.