xf:translate

Replaces all occurrences of $string-var2 with $string-var3 in $string-var1.

If the value of $string-var1, $string-var2, or $string-var3 is the empty sequence, the empty sequence is returned. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.

Signatures

xf:translate(xs:string? $string-var1, xs:string? $string-var2, xs:string? $string-var3) —> xs:string?

Arguments

Data Type
Argument
Description

xs:string?

$string-var1

Represents the source string.

xs:string?

$string-var2

Represents the search string.

xs:string?

$string-var3

Represents the replacement string.

Returns

Returns the value of $string-var1 after all occurrences of the $string-var2 in $string-var1 have been replaced with $string-var3. If the length of $string-var2 (the search string) is less than the length of $string-var3 (replacement string), then only N characters are substituted, where N is length of $string-var2. (See the third example below.)

Returns the value of $string-var1, if $string-var2 is a zero-length string ("").

Examples

Simple

Invoking translate("fghXfgh", "fgh", "yz") returns the string xyXyz as shown in the following example query:

<result>{xf:translate("fghXfgh", "fgh", "yz")}</result> 

The preceding query generates the following result:

<result>yzXyz</result> 

Replacement String Longer Than Source String

Invoking translate("abcdabc", "ab", "ABC") returns the string ABcdABc, as shown in the following example query:

<result>{xf:translate("abcdabc", "ab", "ABC")}</result> 

The preceding query generates the following result:

<result>ABcdABc</result> 

Note: Only the first two characters (AB) of the replacement string ABC are substituted because the length of ab (the search string) is less than the length of ABC (the replacement string). When the length of the search string is less than the length of the replacement string, the length of the search string determines the number characters substituted.

Replacement String Is Empty

Invoking translate("abcdabc", "abc", "") returns the string d because the replacement string is a zero-length string, as shown in the following example query:

<result>{xf:translate("abcdabc", "abc", "")}</result> 

The preceding query generates the following result:

<result>d</result> 

Search String Is Empty

Invoking translate("abcdabc", "", "AB") returns the string abcdabc because the search string is a zero-length string, as shown in the following example query:

<result>{xf:translate("abcdabc", "", "AB")}</result> 

The preceding query generates the following result:

<result>abcdabc</result> 

Pass in Null

Invoking translate((), "ab", "ABC") returns the null string as shown in the following example query:

<result>{xf:translate((), "ab", "ABC")}</result> 

Note: The string: () is the empty sequence (similar to a SQL null) which is a sequence containing zero items.

The preceding query generates the following result:

<result/> 

Related Topics

W3C translate function description.