在XSLT中 Transalate(string,string,string) 函数的用法

本文详细介绍了XSLT中的translate函数使用方法,通过具体示例解释了如何替换字符串中的字符,以及当替换字符串长度不匹配时的行为。文章还提供了一个XSLT样式表示例,展示了translate函数的实际应用。
部署运行你感兴趣的模型镜像

    最近在一个项目中又需要XSLT,所以没办法,又学习了一部分。看到源代码中有一部分有用了一部分的函数,包括,contact(),contains(),translate();前面两个都很好理解的
第三个怎么看怎么不懂,没办法,只好查资料,然后找到了一下这篇E文解释;
“The translate function returns the first argument string with occurrences of characters in the second argument
string replaced by the character at the corresponding position in the third argument string.
For example, translate("bar","abc","ABC") returns the string BAr.
If there is a character in the second argument string with no character at a corresponding position in the third argument
string (because the second argument string is longer than the third argument string),
then occurrences of that character in the first argument string are removed.
For example, translate("--aaa--","abc-","ABC") returns "AAA".
If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character.
If the third argument string is longer than the second argument string, then excess characters are ignored.”

由于才疏学潜,一开始实在难以理解argument和occurrences在这里的意思~所以就慢慢揣摩了,终于功夫不负有心人,发现了这个translate()的用法。

还是举上面的例子比较清晰;
e.g 1. translate("bar","abc","ABC") = BAr
看看第二个参数 abc 和第三个参数 "ABC"

意思是说
把第一个参数的所有a -> A
                  b -> B
      c -> C
    
e.g 2. translate("--aaa--","abc-","ABC") = AAA
这个比较特殊,因为第二个参数的长度大于第三个参数,这就导致了第二个参数的 "-" 没有对应的转换值,规则就是移除第一个参数中的 "-"
其他的跟第一个例子是一样的!

OK...

摘录一篇例子

XSLT 源码

  1.       <xsl:stylesheet
  2.                   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.                   version="2.0"> 
  4.             <xsl:output  method="xml"
  5.                         indent="yes"
  6.                         omit-xml-declaration="yes"/> 
  7.             <xsl:template  match="/aaa"> 
  8.                   <upper> 
  9.                         <xsl:value-of  select="translate(.,'odl','XYZ')"/> 
  10.                   </upper> 
  11.                   <lower> 
  12.                         <xsl:value-of  select="translate(.,'odl','X')"/> 
  13.                   </lower> 
  14.             </xsl:template> 
  15.       </xsl:stylesheet> 

XML 源码

 

  1.  <aaa>Hello, world!</aaa>

Output

  1.   <upper>HeZZX, wXrZY!</upper> 
  2.       <lower>HeX, wXr!</lower> 

 

That's all

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值