xPath expression for Partial String search

Given an XML like the below, one can search for London with the xPath expression using //destination[description='London']/code ... Now my requirement is, how can we search for all descriptions has word "London" which will result me the last 2 elements which are in yellow color

< xml version="1.0" >
<locations>
<destination>
<code>LOH</code>
<description>Loch Ness</description>
</destination>
<destination>
<code>LON</code>
<description>London</description>
</destination>
<destination>
<code>LDY</code>
<description>Londonderry-N.Irl</description>
</destination>
</locations>




Answer this question

xPath expression for Partial String search

  • aleph

    Please use the contains() or starts-with() function:

    //destination[contains(description, 'London')]/code

    Documentation is available here: http://www.w3.org/TR/xpath#section-String-Functions


  • force_fx

    Unfortunately XPath 1.0 doesn't have lower-case() function.

    You can use scripts or extension objects to extend core XPath/XST functionality.

    You can also use translate() XPath function to convert string to lower case.



  • subdigital

    /Root/Lists/NotUsed/Item[contains(., 'peace')='true']


    This sort of works. However it is case sensitive, and for some reason Microsoft .NET 2.0 is crapping out on the lower-case. I thought they supported 2.0 natively. There are several work arounds such as provindg your own lower-case function and a resolver. There might also be an extension that I'm not aware of that may make this function available.


    This is supposed to work to make it case insensitive
    /Root/Lists/NotUsed/Item[contains(lower-case(.), 'peace')='true']

  • xPath expression for Partial String search