Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between translate and replace in XPath

Tags:

xml

xslt

xpath

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

like image 992
user2423959 Avatar asked Aug 17 '16 14:08

user2423959


People also ask

What happens when you translate a string in XPath?

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.

What is the difference between replace () and translate () in JavaScript?

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:

What is the difference between the replace-function and Translate function?

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.

What is the XSLT/XPath reference?

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.


2 Answers

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:

  • replace: ' ' will be replaced with 'XXXXX'
  • translate ' ' will be replaced with the first character of 'XXXXXX' which is 'X'

The explanations of the Oracle-SQL functions might also help (basically the same):

  • replace()
  • translate()
like image 125
Gehtnet Avatar answered Oct 07 '22 06:10

Gehtnet


Difference between translate() and replace()

  • Use translate($s, $mapFrom, $mapTo) to change occurrences of characters given in $mapFrom to those in equivalent positions in $mapTo.
  • Use replace($s, $pattern, $replacement) to change occurrences of matching substrings given by a matching $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.
like image 41
kjhughes Avatar answered Oct 07 '22 06:10

kjhughes