I was going through some XSLT functions and came across two majorly named as translate
and replace
, I understood that, by the end of the day, job of both the functions is replacing some content on declared entity (please enlighten me more on this).
Also I was writing an XSLT where in I want to replace a single value with a bunch of values like below.
<div class="translate">
<xsl:value-of select="translate(current(),' ', 'XXXXX')"/>
</div>
<div class="replace">
<xsl:value-of select="replace(current(),' ', 'XXXXX')"/>
</div>
The translate
is adding only one X
, though I've added XXXXX
, where as the replace is working fine.
Can someone please let me know what's happening in the background?
Here is a working Sample http://xsltransform.net/6rewNxE/2
The first character in XYZ will replace every occurrence of the first character in abc that appears in string. The translated string. 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.
REPLACE () replaces one string with another string. Therefore, if a string contains multiple characters, each character must be in the same order. TRANSLATE () on the other hand, replaces each character one by one, regardless of the order of those characters. There are some cases where both functions will return the same result. Like this:
The replace-function replaces one string with another in a string. If there is the String "abcacb" and you replace "ab" with "xy" you get "xycacb". The translate function replaces the string charcter by character.
XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. The translate function evaluates a string and a set of characters to translate and returns the translated string.
The replace-function replaces one string with another in a string. If there is the String "abcacb" and you replace "ab" with "xy" you get "xycacb".
replace("abcacb","ab","xy") = "xycacb"
The translate function replaces the string charcter by character. The first character in the "please-replace-this-string" will be replaced with the first character in "replace-with-this-string" So if there is the String "abcacb" and you translate "ab" with "xy" you get "xycxcy".
translate("abcacb","ab","xy") = "xycxcy"
For your case:
The explanations of the Oracle-SQL functions might also help (basically the same):
$mapFrom
to those in equivalent positions in $mapTo
.$pattern
regex to a $replacement
string.Note that translate()
is available starting from XPath 1.0; replace()
, from XPath 2.0.
Therefore, in your example:
translate()
will replace each ' '
(space) character with a 'X
'
character, because the $mapTo
character that corresponds to the
equivalent position of ' '
(space) in the $mapFrom
is an 'X
'.replace()
will replace the first " "
(single-space) substring
with a "XXXXX"
, because the literal $pattern
matches the first
occurrence of a " "
(single-space) substring and replaces it with
the full $replacement
string.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With