Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
In an XSLT environment, I usually find that tracing is best performed using the
trace()
only in XQuery. For detailed inspection of values as they are computed, there are a number of good XSLT debuggers available. But occasionally,
trace()
is a useful tool to take out of the kitbag.
See Also
translate
The
translate()
function substitutes characters in a supplied string with nominated replacement characters. It can also be used to remove nominated characters from a string.
For example, the result of
translate(‘ABC-123’,
‘-’,
‘/’)
is the string
ABC/123
.
Changes in 2.0
An empty sequence is not accepted for the second and third arguments, except in backward-compatibility mode.
Signature
Argument | Type | Meaning |
value | xs:string? | The supplied string |
from | xs:string | The list of characters to be replaced, written as a string |
to | xs:string | The list of replacement characters, written as a string |
Result | xs:string? | A string derived from the supplied string, but with those characters that appear in the second argument replaced by the corresponding characters from the third argument, or removed if there is no corresponding character |
Effect
For each character in the supplied string, one of three possible actions is taken:
Note that the third argument must be present, but it can be a zero-length string. In this case, any character present in the second argument is removed from the supplied string.
If a character appears more than once in the list of characters to be replaced, the second and subsequent occurrences are ignored, as are the characters in the corresponding position in the third argument.
If the third argument is longer than the second, excess characters are ignored.
In these rules a
character
means an XML character, not a 16-bit Unicode code. This means that a Unicode surrogate pair (a pair of 16-bit values used to represent a Unicode character in the range
#x10000
to
#x10FFFF
) is treated as a single character, whichever of the three strings it appears in.
Examples
Expression | Result |
translate(“aba12”, “abcd”, “ABCD”) | “ABA12” |
translate(“aba121”, “12”, “”) | “aba” |
translate(“a\b\c.xml”, “\”, “/”) | “a/b/c.xml” |
translate(“5,000.00”, “.,”, “,.”) | “5.000,00” |
Usage and Examples
Many of the XPath 1.0 use cases for the
translate()
function can now be achieved more conveniently in XPath 2.0 by other more powerful functions, such as
matches()
and
replace()
.
In an XSLT stylesheet you might see the
translate()
function being used to perform simple case conversion, for example:
translate($X,
‘abcdefghijklmnopqrstuvwxyz’,
‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’)
This can now be done much better using the
upper-case()
and
lower-case()
functions.
The
translate()
function is useful to remove extraneous punctuation or whitespace; for example, to remove all whitespace, hyphens, and parentheses from a telephone number, write:
translate($X, ‘
()-’, ‘’)
Another use for
translate()
is to test for the presence of a particular character or range of characters. For example, to test whether a string contains a sequence of three or more ASCII digits, write:
contains(translate($X, ‘0123456789’, ‘9999999999’), ‘999’)
Of course, you could do this equally well using
matches($X, ‘[0-9]{3}’)
.
The
translate()
function can be surprisingly powerful. For example, to remove all characters other than digits from a string, you can write:
translate($X, translate($X, ‘0123456789’, ‘’), ‘’)
The inner call on
translate()
strips the digits from
$X
, thus building a list of characters that appear in
$X
and are not digits. The outer call processes
$X
again, this time removing the non-digit characters.
See Also
contains()
on page 730
matches()
on page 828
replace()
on page 862
substring()
on page 883
substring-after()
on page 885
substring-before()
on page 887
true
This function returns the boolean value
true
.
Changes in 2.0
None.
Signature
This function takes no arguments.
Type | Meaning | |
Result | xs:boolean | The xs:boolean value true |
Effect
There are no boolean constants available in XPath expressions, so the functions
true()
and
false()
can be used where a constant boolean value is required.
Usage
The most common occasion where constant boolean values are required is when supplying an argument to a function or to an XSLT template. See the example below.
XSLT Example
The following code calls a named template, setting the parameter
verbose
to
true
: