Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath lower-case() function

I'm using XPATH to select certain nodes from an XML document.

The user is able to insert a value for the location. It's working fine, but it does not work if different cases are used.

I've decided that changing both the XML values and the user's input to lower case before being compared is probably the best way to go about it.

I've got this as my selector at the moment:

NodeIter = nav.Select("/Houses/House/location[contains(../location, '" + location_input + "')]");

I've tried putting the lower-case() function in various locations, but it isn't happy with it.

How do I make it so that the value of ../location is compared as lower case?

Note: location_input is set to lower using ToLower() within my c# code.

like image 320
Luke Avatar asked Jan 23 '12 18:01

Luke


1 Answers

The lower-case() function is only supported from XPath 2.0 onwards. If your environment supports this version of the standard, you can write:

NodeIter = nav.Select("/Houses/House/location[contains(lower-case(.), '"
    + location_input + "')]");

However, chances are you're stuck with XPath 1.0. In that case, you can abuse the translate() function:

NodeIter = nav.Select("/Houses/House/location[contains(translate(., "
    + "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '"
    + location_input + "')]");
like image 190
Frédéric Hamidi Avatar answered Oct 07 '22 13:10

Frédéric Hamidi