translate

Replaces individual characters

Description

The fn:translate function replaces individual characters of a string with other individual characters. The $mapString argument is a list of characters to be changed, and $transString is the list of replacement characters. Each character in $mapString is replaced by the character in the same position in $transString. If $mapString is longer than $transString, the characters in $mapString that have no corresponding character in $transString are not included in the result. Characters in the original string that do not appear in $mapString are copied to the result unchanged.

Note that this function is only for replacing individual characters with other individual characters, or removing individual characters. If you want to replace sequences of characters, you should use the fn:replace function instead. This function is sometimes used for translating strings between lowercase and uppercase, but the fn:upper-case and fn:lower-case functions do this more robustly based on Unicode mappings.

Parameters

Description

arg:string()

the string to translate

mapString:string()

the string of “from” characters

transString:string()

the string of “to” characters

Examples

XPath

Results

translate(‘1999/01/02’, ‘/’, ‘-‘)

1999-01-02

translate(‘xml query’, ‘qlmx’, ‘QLMX’)

XML Query

translate(‘xml query’, ‘qlmx ‘, ‘Q’)

Query

translate(‘xml query’, ‘qlmx ‘, ‘’)

uery

translate(‘xml query’, ‘abcd’, ‘ABCD’)

xml query

translate(‘’, ‘qlmx ‘, ‘Q’)

zero-length string

translate((), ‘qlmx ‘, ‘Q’)

zero-length string