XPath and case sensitive problems

Running xpaths against a xml doc (comming from a mssql server) and having trouble with case sensitive letters.

I have tried to use the xml translate funktion, but it would need an exact match in the DB anyway. If I look for the name John and it is typed JohN in the DB I get nothing. Is the only solution to 'wash' the data in the DB.

Are there any work around to make a xpath query non case sensitive 


Answer this question

XPath and case sensitive problems

  • admoloc

    XPATH 2.0 (next version) has functions to deal with case-insensitivity, but unfortunately the current version does not. Your option is to try to incorporate a 3rd party solution such as Exslt from GotDotNet.com.

  • jimmyshu

    For further information see:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnxml/html/practexslt.asp

    You can use the str2:uppercase function to make your comparisons case-insensitive.

  • donald_hodges

    I'am pretty new to XSL. Been trying the following generic XSL

    < xml version="1.0" encoding="UTF-8" >
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts">
    <msxsl:script language="JScript" implements-prefix="user">
    <![CDATA[
    function uppercase(r)
    {
    r.toUpperCase();
    }
    ]]>
    </msxsl:script>
    <xsl:template match="*">
    <xsl:value-of select="user:uppercase(string(.))"/>
    </xsl:template>
    </xsl:stylesheet>

    This code transforms all text generic to upper case but drops out alle the elements, so there is no tags left in the browser view. Any idea on how to keep xml element and just translate their text to uppercase.

  • vgrigor

    Yes got it without XSL but by doing a transform on the element instead of the node.

    looking like this:

    xpath = @"//Person[" + "translate("+sCol+",'abcdefghijklmnopqrstuvwxyzaoa','ABCDEFGHIJKLMNOPQRSTUVWXYZAOA')" + "='"+sValue.ToUpper()+"']";

    where sCol is the element and sValue is the search value.

    Thx,
    Jakob


  • BANND

    But, if you make both sides of your comparison uppercase, then it is case-insensitive. For example (from an application I wrote in the past):

    expr = nav.Compile(String.Format("//{0}[str2:uppercase(.)='{1}']", elementName, searchText.ToUpper()));


  • Alex61291

    not quite, it only makes the xpath query uppercase. The text I want to query with xpath might not be upper case.

    xpath: //Person[starts-with(Name, 'John')]

    or

    xpath: //Person[starts-with(Name, translate('John', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]

    I might have a entery where the user wrote JohN in the database. JohN will not be found.

    Has I see it str2:uppercase or the translate function will not make a xpath search case-insensitive.

  • klaus klaussen

    This sound interesting, could you translate your sample to my sample where xpath=

    //Person[starts-with(Name, 'John')]

    I cannot see how I can access the XML doc element text, to force it to upper case.

    Thx,
    Jim

  • Atomican

    I would think that the following would work for you after you setup xslt.

    //Person[starts-with(str2:uppercase(Name), 'JOHN')]

    The only part I'm not certain about is Name, it appears that is a sub-element of Person in your XML and I'm not sure of the correct syntax to specify that in the XPATH query as I usually have to play around and tweak some when creating XPATH queries. The period in my query represent the text of the current element, which wouldn't apply for you. If name were an attribute it would be "@Name" here, but if it is a subelement you may have to specify "child::Name" in its place.

  • XPath and case sensitive problems