Difficulty: ★☆☆ (easy)
Keywords: lowercase, uppercase

Problem

You need to convert a text string from lowercase or uppercase, respectively, taking care of the current locale.

Solution

Use the utility template string.lower or string.upper from the DocBook stylesheets:

<xsl:call-template name="string.upper">
   <xsl:with-param name="string">This is your string</xsl:with-param>
</xsl:call-template>

Or:

<xsl:call-template name="string.lower">
   <xsl:with-param name="string">This is your string</xsl:with-param>
</xsl:call-template>

Discussion

To transform a string into uppercase letters, XPath offers the translate function. The translate function expects three parameters: the string you want to change, a string of lowercase letters, and a string of uppercase letters. Your code looks like this:

<xsl:value-of select="translate($string, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

When called by the XSLT processor, the content of the string variable is transformed into uppercase. This works pretty well if your language is English or your string does not contain any accented characters. However, if your string contains, for example, the German lowercase letter “ö”, translate does not transform it into the uppercase letter “Ö”.

This is handled by the above templates string.lower and string.upper. They use the language files of the DocBook stylesheets and get the lowercase and uppercase letters from so-called “gentext templates”. The uppercase.alpha and lowercase.alpha entries are used in the current locale.

However, this transformation is not always perfect. For example, it is recommended to transform the German “ß” (lowercase) into “SS” (uppercase).[7] This makes the translate function unusable for such corner cases. Only XPath 2.0 supports locale-sensitive mappings with fn:upper-case.

See Also


[7] Since 2008, Unicode contains the capital letter sz ẞ (see http://en.wikipedia.org/wiki/Capital_%E1%BA%9E which is located at position U+1E9E. Most fonts contain this glyphs now, but not all. You should carefully investigate your fonts before you change anything. For a German keyboard layout under X11, press Caps Lock+ß to display the capital sharp ß which is printed as “ẞ”.


Project@GitHubIssue#7