Archive for tag: XPath

Trim function or normalize space in XPath with C#

In XPath we can trim or remove spaces, it doesn't have trim function in XPath 1.0 but can use normalize-space(), it just doesn't trim it doesn't it more thing that as well following is an example:

normalize-space()

//removing spaces from beginning and end
NodeIter = nav.Select("/Products/Product/name[normalize-space(' Item1 ') ='Item1']");

//removing only trailing spaces
NodeIter = nav.Select("/Products/Product/name[normalize-space(' Item 1 ') ='Item 1']");

//removing more than one spaces and leave just one space between characters or words
NodeIter = nav.Select("/Products/Product/name[normalize-space(' Item    1   2 ') ='Item 1 2']");

 

normalize-space() does more than just trimming spaces.

If you have question don't forget to ask.

Thank you for going through

XPath value lower case or upper case function in C#

XPath function to translate or convert into lower case using two different ways based on version of XPath.

In XPath 2.0 onwards it has function lower-case() or upper-case(). If your environment supports this version of the standard, you can write:

Lower Case:

NodeIter = nav.Select("/Products/Product/name[lower-case(.) ='" + productName +"']")

Upper Case:

NodeIter = nav.Select("/Products/Product/name[upper-case(.) ='" + productName +"']")

However, if your system supports XPath version 1.0. In that case, you can use the translate() function:

Lower Case:

NodeIter = nav.Select("("/Products/Product/name[ (translate(., "
    + "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '"
    + productName + '"]");

Upper Case:

NodeIter = nav.Select("("/Products/Product/name[ (translate(., "
    + "'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '"
    + productName + '"]");

If you have any question don't forget to ask
Thank you for watching this article