Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Functions operating on strings (9/15) >

Replacing characters in strings with translate() function

Function translate() replaces characters in the second argument with the characters in the third one. If the second argument is longer than the third one, characters from the second argument which have no counterpart in the third one are deleted.

Consult replace() if you want to replace a character by several characters or a substring by a different one.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <upper>
                        <xsl:value-of  select="translate(.,'odl','XYZ')"/>
                  </upper>
                  <lower>
                        <xsl:value-of  select="translate(.,'odl','X')"/>
                  </lower>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>Hello, world!</aaa>
Output

      <upper>HeZZX, wXrZY!</upper>
      <lower>HeX, wXr!</lower>


Previous chapter: Arithmetics
Next chapter: Transforming strings with regular expressions
Previous page: Transforming strings to lowercase and uppercase
Next page: Normalization of whitespace characters in a string with normalize-space function