XML technologies / XSLT / XSLT Cookbook / Strings / Converting letter case

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to convert an uppercase string to lowercase, or vice versa.

Solution

XSLT 1.0

Use XSLT function translate( ). This code, for example, converts uppercase to lowercase:

translate($input, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')

This example converts lowercase to uppercase:

translate($input, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')

XSLT 2.0

Use the XPath 2.0 functions upper-case( ) and lower-case( ):

upper-case($input) lower-case($input)

Discussion

This recipe is trivial, of course, but is included to discuss the drawbacks of the XSLT 1.0 solution. Case conversion is trivial as long as the text is limited to one locale. English rarely requires handling accented characters or complex conversions in which one character becomes two. German is a prime example: lowercase ß becomes uppercase SS. Most modern languages provide locale-aware case conversion. XSLT does not directly support this concept, unfortunately, especially given its other features supporting internationalization.

A small improvement is to define general XML entities for each conversion, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [ 
  <!ENTITY UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"> 
  <!ENTITY LOWERCASE "abcdefghijklmnopqrstuvwxyz"> 
  <!ENTITY UPPER_TO_LOWER " '&UPPERCASE;' , '&LOWERCASE;' "> 
  <!ENTITY LOWER_TO_UPPER " '&LOWERCASE;' , '&UPPERCASE;' "> 
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>     
  <xsl:template match="/">
    <xsl:variable name="test" select=" 'The rain in Spain falls mainly on the plain' "/>
    <output>
      <lowercase>
        <xsl:value-of select="translate($test,&UPPER_TO_LOWER;)"/>
      </lowercase>
      <uppercase>
        <xsl:value-of select="translate($test,&LOWER_TO_UPPER;)"/>
      </uppercase>
    </output>
  </xsl:template>      
</xsl:stylesheet>

These entity definitions accomplish three things. First, they make porting to another locale easier, since only the definitions of entities UPPERCASE and LOWERCASE need changing. Second, they make code more compact because the alphabet no longer needs listing twice. Third, they clarify the purpose of the translate call to code reviewers. Purists might object to the disappearance of the third parameter of translate( ), but I like how the code reads. If you prefer strict purity, use translate($test, &UPPERCASE;, &LOWERCASE;).

I have rarely seen entities used in other XSLT books, but believe the technique has merit. An advantage of XSLT being written in XML syntax is that you can exploit every XML feature, and entity definitions are certainly useful. If you plan to use this technique in more than a few stylesheets, consider placing common entity definitions in an external file and including them as in example Standard.ent. Alternatively, store the values as global variables in an external stylesheet and import them as needed. Many XSLT veterans prefer this alternative.

Example: Standard.ent

<!ENTITY UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ">
<!ENTITY LOWERCASE "abcdefghijklmnopqrstuvwxyz">
<!ENTITY UPPER_TO_LOWER " '&UPPERCASE;' , '&LOWERCASE;' ">
<!ENTITY LOWER_TO_UPPER " '&LOWERCASE;' , '&UPPERCASE;' ">
<!-- andere ... --> 

Then use a parameter entity defined in relation to the external file standard.ent, as in the following example.

Example: A stylesheet using standard.ent.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE stylesheet [ 
  <!ENTITY % standard SYSTEM "standard.ent">
  %standard;
]>
<xsl:stylesheet version="1.0"
  <!-- ... --> 
</xsl:stylesheet> 

Steve Ball’s case-conversion implementation works in practically every case by including the commonest Unicode characters in the lowercase and uppercase strings and treating German ß separately.

XSLT 2.0

The new XPath 2.0 functions upper-case( ) and lower-case( ) solve most case-conversion problems in non-English alphabets. Locale-sensitive Unicode conversions are an exception. Avoid these functions for case-insensitive comparisons; instead use compare( ) with a case-insensitive collation.

See also

Steve Ball’s solution is available in the “XSLT Standard Library”.

  

 

 

 

back to top