Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lowercase conversion in XSL

Tags:

lowercase

xslt

I have an XML like

<emps>
<emp id='3432'>
 <fname>Jack</fname>
 <lname>Dawson</lname>
<emp>
<emp id='1122'>
 <fname>Jack</fname>
 <lname>Thompson</lname>
<emp>
<emps>

I am developing a web application which searches this xml based on the first name entered and comes up with a resultant page. To achieve this I have written an xslt to transform the XML to HTML based on the input search string which is passed as a variable named srchStr.

<xsl:template match="employees">
  <xsl:for-each select="emp[fname=$srchStr]">
<tr>
   <xsl:variable name="id">
    <xsl:value-of select="@id" />
   </xsl:variable>
   <td>
    <a href='detailSearch.do?id={$id}'>
     <xsl:value-of select="fname" />
     ,
     <xsl:value-of select="lname" />
    </a>
   </td>

  </tr>
</xsl:for-each
</xsl:template>

But the user may enter the name either in upper case or lower case. So how to convert the first name inside the xml tag fname to lower case and do the comparison?

Can some one put a code snippet to use fn:lower-case inside my xsl.

like image 471
Bala Avatar asked Jun 16 '10 13:06

Bala


2 Answers

To convert a string to lower case or uppercase you can use the XPath 1.0 function translate:

First define your alphabets for lower case and upper case letters. Note that the position of each pair of characters needs to be the same:

<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

Then you can convert to upper case:

<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>

or to lower case

<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
like image 196
Dirk Vollmar Avatar answered Nov 17 '22 02:11

Dirk Vollmar


emp[lower-case(fname)=lower-case($srchStr)]

Or, if you have XPath 1.0 only, you may try using translate like here: http://geekswithblogs.net/TimH/archive/2006/07/06/84229.aspx

Be warned though, the example with translate would not work on names with accents (like mine :)

like image 4
František Žiačik Avatar answered Nov 17 '22 01:11

František Žiačik