xml XPath 中的翻译和替换有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38998970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between translate and replace in XPath
提问by user2423959
I was going through some XSLT functions and came across two majorly named as translateand 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).
我正在浏览一些 XSLT 函数,遇到了两个主要命名为translateand 的函数replace,我明白,到一天结束时,这两个函数的工作正在替换声明实体上的一些内容(请多多指教)。
Also I was writing an XSLT where in I want to replace a single value with a bunch of values like below.
此外,我正在编写一个 XSLT,其中我想用如下所示的一堆值替换单个值。
<div class="translate">
<xsl:value-of select="translate(current(),' ', 'XXXXX')"/>
</div>
<div class="replace">
<xsl:value-of select="replace(current(),' ', 'XXXXX')"/>
</div>
The translateis adding only one X, though I've added XXXXX, where as the replace is working fine.
该translate是只增加一个X,虽然我已经添加了XXXXX,在那里作为替换工作正常。
Can someone please let me know what's happening in the background?
有人可以让我知道后台发生了什么吗?
Here is a working Sample http://xsltransform.net/6rewNxE/2
回答by kjhughes
Difference between translate() and replace()
translate() 和 replace() 的区别
- Use translate($s, $mapFrom, $mapTo)to change occurrences of
charactersgiven in
$mapFromto those in equivalent positions in$mapTo. - Use replace($s, $pattern, $replacement)to change
occurrences of matching substringsgiven by a matching
$patternregex to a$replacementstring.
- 使用翻译($ S,$ mapFrom,$ mapTo) ,以改变出现
的字符在给
$mapFrom那些在等效位置$mapTo。 - 使用replace($s, $pattern, $replacement)将匹配正则表达式给出
的匹配子字符串的出现次数更改
$pattern为$replacement字符串。
Note that translate()is available starting from XPath 1.0; replace(), from XPath 2.0.
请注意,translate()从 XPath 1.0 开始可用;replace(),来自 XPath 2.0。
Therefore, in your example:
因此,在您的示例中:
translate()will replace each' '(space) character with a 'X' character, because the$mapTocharacter that corresponds to the equivalent position of' '(space) in the$mapFromis an 'X'.replace()will replace the first" "(single-space) substring with a"XXXXX", because the literal$patternmatches the first occurrence of a" "(single-space) substring and replaces it with the full$replacementstring.
translate()将每个' '(空格)字符替换为一个 'X' 字符,因为$mapTo对应于' '(空格)在 中的等效位置的字符$mapFrom是一个 'X'。replace()将用 a 替换第一个" "(单空格)子字符串"XXXXX",因为文字$pattern匹配第一次出现的" "(单空格)子字符串并将其替换为完整$replacement字符串。
回答by Gehtnet
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".
替换函数用字符串中的另一个字符串替换一个字符串。如果有字符串“abcacb”并且您将“ab”替换为“xy”,则会得到“xycacb”。
replace("abcacb","ab","xy") = "xycabc"
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 函数逐个字符替换字符串。“please-replace-this-string”中的第一个字符将被替换为“replace-with-this-string”中的第一个字符所以如果有字符串“abcacb”并且你将“ab”翻译成“xy”你得到“xycxcy”。
translate("abcacb","ab","xy") = "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'
- 替换:' ' 将被替换为 'XXXXX'
- 翻译 ' ' 将被替换为 'XXXXXX' 的第一个字符,即 'X'
The explanations of the Oracle-SQL functions might also help (basically the same):
Oracle-SQL 函数的解释也可能有所帮助(基本相同):

