Skip to content

Commit 9cf4a4a

Browse files
Improve XSLT translate description (mdn#40540)
* Improve XSLT translate description * Apply suggestions from code review * Update index.md * Update files/en-us/web/xml/xpath/reference/functions/translate/index.md --------- Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
1 parent 66f1ba7 commit 9cf4a4a

File tree

1 file changed

+15
-15
lines changed
  • files/en-us/web/xml/xpath/reference/functions/translate

1 file changed

+15
-15
lines changed

files/en-us/web/xml/xpath/reference/functions/translate/index.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,38 @@ The translated string.
2828

2929
## Description
3030

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.
3232

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.
3436

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).
3638

37-
```xml
38-
<xsl:value-of select="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-of select="translate('The quick brown fox', 'brown', 'red')" />
3943
```
4044

41-
Output
45+
## Examples
4246

43-
```plain
44-
THE QUICK BROWN FOX.
45-
```
47+
### Using `translate()` for case conversion
4648

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.
4850

49-
Example
51+
However, this is the closest we have at present to a function that can convert a string to uppercase or lowercase.
5052

5153
```xml
52-
<xsl:value-of select="translate('The quick brown fox.', 'brown', 'red')" />
54+
<xsl:value-of select="translate('The quick brown fox.', 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
5355
```
5456

5557
Output
5658

5759
```plain
58-
The quick red fdx.
60+
THE QUICK BROWN FOX.
5961
```
6062

61-
- If `XYZ` contains more characters than `abc`, the extra characters are ignored.
62-
6363
## Specifications
6464

6565
[XPath 1.0 4.2](https://www.w3.org/TR/xpath-10/#function-translate)

0 commit comments

Comments
 (0)