translate Function

Returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string.

string translate(string, string, string)

Remarks

This function provides a mapping of characters on the first argument. The second and third arguments describe the scheme of the mapping.

The following function call returns "BAr":

translate("bar","abc","ABC")

If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), occurrences of that character in the first argument string are removed.

The following function call returns "AAA":

translate("--aaa--","abc-","ABC")

If a character occurs more than once in the second argument string, the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, excess characters are ignored.

If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated.

Caution noteCaution

String conversions for node sets that are passed as arguments to this function may yield unexpected results. For more information, see string Function.

This function is case-sensitive.

Example

This example demonstrates the preceding translate() expressions.

XML File

None; the XSLT file calls itself.

XSLT File (translate.xsl)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="translate.xsl"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
   translate("bar","abc","ABC") = 
      <xsl:value-of select='translate("bar","abc","ABC")'/>
   <br/>translate("--aaa--","abc-","ABC") = 
      <xsl:value-of select='translate("--aaa--","abc-","ABC")'/>
</xsl:template>

</xsl:stylesheet>

Formatted Output

translate("bar","abc","ABC") = BAr translate("--aaa--","abc-","ABC") = AAA

Processor Output

<?xml version="1.0" encoding="UTF-16"?>   translate("bar","abc","ABC") =       BAr<br />translate("--aaa--","abc-","ABC") =       AAA

See Also

Reference

XML Data Types Reference