You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/xml/xpath/reference/functions/translate/index.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,38 +28,38 @@ The translated string.
28
28
29
29
## Description
30
30
31
-
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.
31
+
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.
32
32
33
-
However, this is the closest we have at present to a function that can convert a string to uppercase or lowercase.
33
+
- If `abc` is longer than `XYZ`, then the extra characters at the end of `abc` are mapped to the empty string (i.e., they are removed from the source string).
34
+
- If `XYZ` contains more characters than `abc`, the extra characters are ignored.
35
+
- If a character appears multiple times in `abc`, then the first occurrence determines the replacement character.
34
36
35
-
Example
37
+
`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).
36
38
37
-
```xml
38
-
<xsl:value-ofselect="translate('The quick brown fox.', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
39
+
A substitution like this will not output `The quick red fox` as you might expect; instead the result is `The quick red fdx`.
40
+
41
+
```xml example-bad
42
+
<xsl:value-ofselect="translate('The quick brown fox', 'brown', 'red')" />
39
43
```
40
44
41
-
Output
45
+
## Examples
42
46
43
-
```plain
44
-
THE QUICK BROWN FOX.
45
-
```
47
+
### Using `translate()` for case conversion
46
48
47
-
- If `abc` is longer than `XYZ`, then every occurrence of characters in `abc` that do not have a corresponding character in `XYZ` will be removed.
49
+
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.
48
50
49
-
Example
51
+
However, this is the closest we have at present to a function that can convert a string to uppercase or lowercase.
50
52
51
53
```xml
52
-
<xsl:value-ofselect="translate('The quick brown fox.', 'brown', 'red')" />
54
+
<xsl:value-ofselect="translate('The quick brown fox.', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
53
55
```
54
56
55
57
Output
56
58
57
59
```plain
58
-
The quick red fdx.
60
+
THE QUICK BROWN FOX.
59
61
```
60
62
61
-
- If `XYZ` contains more characters than `abc`, the extra characters are ignored.
0 commit comments