131

How do you do case conversion in XSL?

<xsl:variable name="upper">UPPER CASE</xsl:variable>
<xsl:variable name="lower" select="???"/>
mjs
  • 63,493
  • 27
  • 91
  • 122

6 Answers6

214

In XSLT 1.0 the upper-case() and lower-case() functions are not available. If you're using a 1.0 stylesheet the common method of case conversion is translate():

<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />


<xsl:template match="/">
  <xsl:value-of select="translate(doc, $lowercase, $uppercase)" />
</xsl:template>
wasmachien
  • 969
  • 1
  • 11
  • 28
Jon W
  • 15,480
  • 6
  • 37
  • 47
  • 5
    Is there a Unicode version? This solution is not going to work with funny characters... – mjs Feb 25 '09 at 15:06
  • 1
    For XSLT 1.0 you'd have to add more to the smallcase/uppercase variables or use an extension function. – Jon W Feb 25 '09 at 15:09
  • 1
    If you decided not to use the extention function you might be able to make a complete list using the Unicode Character Database: http://www.unicode.org/Public/UNIDATA/UCD.html – Jon W Feb 25 '09 at 15:15
  • 16
    Here is the complete ANSI table: translate(%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ', 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ') – A.Game Feb 06 '15 at 09:33
49

XSLT 2.0 has upper-case() and lower-case() functions. In case of XSLT 1.0, you can use translate():

<xsl:value-of select="translate("xslt", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")" />
j0k
  • 22,600
  • 28
  • 79
  • 90
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
10

.NET XSLT implementation allows to write custom managed functions in the stylesheet. For lower-case() it can be:

<?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:utils="urn:myExtension" exclude-result-prefixes="msxsl">

  <xsl:output method="xml" indent="yes"/>

  <msxsl:script implements-prefix="utils" language="C#">
    <![CDATA[
      public string ToLower(string stringValue)
      {
        string result = String.Empty;

        if(!String.IsNullOrEmpty(stringValue))
        {
          result = stringValue.ToLower(); 
        }

        return result;
      }
    ]]>
  </msxsl:script>

  <!-- using of our custom function -->
  <lowercaseValue>
    <xsl:value-of select="utils:ToLower($myParam)"/>
  </lowercaseValue>

Assume, that can be slow, but still acceptable.

Do not forget to enable embedded scripts support for transform:

// Create the XsltSettings object with script enabled.
XsltSettings xsltSettings = new XsltSettings(false, true);

XslCompiledTransform xslt = new XslCompiledTransform();

// Load stylesheet
xslt.Load(xsltPath, xsltSettings, new XmlUrlResolver());
Vladislav
  • 1,696
  • 27
  • 37
  • 4
    This solution will work with any characters, even weird Unicode ones. This worked great for me, but I simplified the method body to `return (stringValue ?? string.Empty).ToLower();`. Thanks @Vladislav, I never knew you could do that! – Joe Skeen Sep 19 '14 at 20:43
2
<xsl:variable name="upper">UPPER CASE</xsl:variable>
<xsl:variable name="lower" select="translate($upper,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
<xsl:value-of select ="$lower"/>

//displays UPPER CASE as upper case
1

For ANSI character encoding:

 translate(//variable, 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ', 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ')
Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43
1

upper-case(string) and lower-case(string)

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177