<html>
<head>
<title>translate() Function</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">translate() Function</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Allows you to convert individual characters in a string from one value to another. In many languages, this function is powerful enough to convert characters from one case to another. </td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Inputs</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Three strings. The first is the original, untranslated string, and the second and third strings define the characters to be converted. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Output</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>The original string, translated as follows:</p>
<ul>
        <dd>If a character in the original string appears in the second argument string, it is replaced with the corresponding character in the third argument string. In other words, if the character <span class="LITERAL">J</span> appears in the original string and <span class="LITERAL">J</span> appears as the fourth character in the second argument string, the <span class="LITERAL">J</span> is replaced with the fourth character from the third argument string. (Don't worry, we'll have some examples to clear this up in just a minute.)</p>
        </li>
        <dd>If a character in the original string appears in the second argument string and there is no corresponding character in the third argument string (the second argument string is longer than the third), then that character is deleted. In other words, if the character <span class="LITERAL">J</span> appears in the original string, and <span class="LITERAL">J</span> appears as the fourth character in the second argument string, and the third argument string is three characters long, the <span class="LITERAL">J</span> is deleted. </p>
        </li>
        <dd>If a character in the second argument string appears more than once, the first occurrence determines the replacement character.</p>
        </li>
        <dd>If the third argument string is longer than the second argument string, the extra characters are ignored. </p>
        </li>
      </ul>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XPath section 4.2, String Functions. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Here's a stylesheet with several examples of the <span class="LITERAL">translate()</span> function:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="text"/&gt;

  &lt;xsl:variable name="newline"&gt;
&lt;xsl:text&gt;
&lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Tests of the translate() function:&lt;/xsl:text&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Convert a string to uppercase:&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   translate('Doug', 'abcdefghijklmnopqrstuvwxyz', &lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;             'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=&lt;/xsl:text&gt;
    &lt;xsl:value-of select="translate('Doug', 
      'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Convert a string to lowercase:&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', &lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;             'abcdefghijklmnopqrstuvwxyz')=&lt;/xsl:text&gt;
    &lt;xsl:value-of 
      select="translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                        'abcdefghijklmnopqrstuvwxyz')"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Remove parentheses, spaces, and dashes&lt;/xsl:text&gt;
    &lt;xsl:text&gt; from a U.S. phone number:&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   translate('(555) 555-1212', '() -', '')=&lt;/xsl:text&gt;
    &lt;xsl:value-of select="translate('(555) 555-1212', '() -', '')"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Replace all but the last four digits of a &lt;/xsl:text&gt;
    &lt;xsl:text&gt;credit card number with Xs:&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:variable name="credit" select="'4918 3829 9920 1810'"/&gt;
    &lt;xsl:text&gt;   $credit='&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$credit"/&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   translate(substring($credit, 1, 15), &lt;/xsl:text&gt;
    &lt;xsl:text&gt;'1234567890 ', 'XXXXXXXXXX-')&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring($credit, 16)&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   The first part is &lt;/xsl:text&gt;
    &lt;xsl:value-of 
      select="translate(substring($credit, 1, 15), '123457890 ', 
        'XXXXXXXXX-')"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   The second part is &lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring($credit, 16)"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   Here's how they look together: &lt;/xsl:text&gt;
    &lt;xsl:value-of 
      select="translate(substring($credit, 1, 15), '123457890 ', 
        'XXXXXXXXX-')"/&gt;
    &lt;xsl:value-of select="substring($credit, 16)"/&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>When we use this stylesheet with any XML document, here are the results:</p>
<span class="PROGRAMLISTING"><pre>

Tests of the translate() function:

Convert a string to uppercase:
   translate('Doug', 'abcdefghijklmnopqrstuvwxyz',
             'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=DOUG

Convert a string to lowercase:
   translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
             'abcdefghijklmnopqrstuvwxyz')=doug

Remove parentheses, spaces, and dashes from a U.S. phone number:
   translate('(555) 555-1212', '() -', '')=5555551212

Replace all but the last four digits of a credit card number with Xs:
   $credit='4918 3829 9920 1810'
   translate(substring($credit, 1, 15), '1234567890 ', 'XXXXXXXXXX-')
   substring($credit, 16)

   The first part is XXXX-XXXX-XXXX-
   The second part is 1810

   Here's how they look together: XXXX-XXXX-XXXX-1810
</pre></span>
</td>
</tr>
</table>
</div>
</body>
</html>