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

    最近在一个项目中又需要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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值