How do i make Xpath search case insensitive

后端 未结 4 929
迷失自我
迷失自我 2020-12-11 15:20

I\'m currently making a xpath search, I\'ve got the the search working but I need to make it case insensitive. The xml file I\'m using is 1.0 which from my research means I\

4条回答
  •  抹茶落季
    2020-12-11 16:22

    The currently accepted answer is flawed -- because nothing guarantees that the second argument of contains() is already converted to lower case.

    Also, it uses '$search' -- and this is literally the string "$search" -- not the variable $search.

    Here is a correct solution:

    //channel/item
       [contains(translate(., 
                           'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 
                           'abcdefghjiklmnopqrstuvwxyz'),
                 translate($txtSearch, 
                           'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 
                           'abcdefghjiklmnopqrstuvwxyz')
                 )
       ]
    

    The corresponding XPath 2.0 expression:

    //channel/item[contains(lower-case(.), lower-case($txtSearch))]
    

    Update:

    Based on this solution, @alain.janinm has corrected his answer.

提交回复
热议问题