| translate() Function | |
| 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. | |
| Inputs | |
|
Three strings. The first is the original, untranslated string, and the second and third strings define the characters to be converted. |
|
| Output | |
|
The original string, translated as follows:
|
|
| Defined in | |
|
XPath section 4.2, String Functions. |
|
| Example | |
|
Here's a stylesheet with several examples of the translate() function:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$newline"/>
<xsl:text>Tests of the translate() function:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text>Convert a string to uppercase:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> translate('Doug', 'abcdefghijklmnopqrstuvwxyz', </xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=</xsl:text>
<xsl:value-of select="translate('Doug',
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text>Convert a string to lowercase:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', </xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> 'abcdefghijklmnopqrstuvwxyz')=</xsl:text>
<xsl:value-of
select="translate('Doug', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz')"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text>Remove parentheses, spaces, and dashes</xsl:text>
<xsl:text> from a U.S. phone number:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> translate('(555) 555-1212', '() -', '')=</xsl:text>
<xsl:value-of select="translate('(555) 555-1212', '() -', '')"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text>Replace all but the last four digits of a </xsl:text>
<xsl:text>credit card number with Xs:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:variable name="credit" select="'4918 3829 9920 1810'"/>
<xsl:text> $credit='</xsl:text>
<xsl:value-of select="$credit"/>
<xsl:text>'</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> translate(substring($credit, 1, 15), </xsl:text>
<xsl:text>'1234567890 ', 'XXXXXXXXXX-')</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:text> substring($credit, 16)</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text> The first part is </xsl:text>
<xsl:value-of
select="translate(substring($credit, 1, 15), '123457890 ',
'XXXXXXXXX-')"/>
<xsl:value-of select="$newline"/>
<xsl:text> The second part is </xsl:text>
<xsl:value-of select="substring($credit, 16)"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:text> Here's how they look together: </xsl:text>
<xsl:value-of
select="translate(substring($credit, 1, 15), '123457890 ',
'XXXXXXXXX-')"/>
<xsl:value-of select="substring($credit, 16)"/>
</xsl:template>
</xsl:stylesheet>
When we use this stylesheet with any XML document, here are the results:
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
|
|