translate
The translate
function evaluates a string and a set of characters to translate and returns the translated string.
Syntax
translate(string, abc, XYZ)
Parameters
Return value
The translated string.
Description
For every character in string
, if abc
contains that character, it is replaced with the character at the same index in XYZ
. If abc
does not contain that character, it is kept as-is.
- If
abc
is longer thanXYZ
, then the extra characters at the end ofabc
are mapped to the empty string (i.e., they are removed from the source string). - If
XYZ
contains more characters thanabc
, the extra characters are ignored. - If a character appears multiple times in
abc
, then the first occurrence determines the replacement character.
translate()
is a character-by-character substitution function, not a regexp or string replacement function. The abc
and XYZ
strings represent character ciphers, not substrings. This means that if you run into any of the cases above, you may be using the method incorrectly (except perhaps having a longer abc
to remove certain characters).
A substitution like this will not output The quick red fox
as you might expect; instead the result is The quick red fdx
.
<xsl:value-of select="translate('The quick brown fox', 'brown', 'red')" />
Examples
Using translate()
for case conversion
XPath notes that the translate function is not a sufficient solution for case conversion in all languages. A future version of XPath may provide additional functions for case conversion.
However, this is the closest we have at present to a function that can convert a string to uppercase or lowercase.
<xsl:value-of select="translate('The quick brown fox.', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
Output
THE QUICK BROWN FOX.
Specifications
Gecko support
Supported.