【问题标题】:Replace Multiple Characters in a String (XSLT)替换字符串中的多个字符 (XSLT)
【发布时间】:2009-10-12 23:32:25
【问题描述】:

我需要能够替换某些字符,以便可以将它们用作 CSS 类。

我有诸如 class(name)、class&name 等字符串,它们不是有效的 CSS 类(据我所知)。

如何使用替换功能替换多个字符,

例如

translate(className, ' ','') (would replace a space)

但是是否可以对多个字符执行此操作?

翻译似乎不适用于 &

例子

XML

<title>Mary & the Wolf<title>

XSLT

<xsl:value-of select="translate(title, ' &','')"/></xsl:attribute>

所以我希望输出是:

MarytheWolf

但目前我收到 & 字符错误。

【问题讨论】:

    标签: xslt


    【解决方案1】:

    translate() 按字符工作:

    translate(className, ' &#?!','')  // would remove any character in the string #1
    

    translate(className, ' &#?!','_____')  // would replace any character 
                                           // in the string #1 with '_'
    

    【讨论】:

    • 谢谢,用 char 代替似乎是个好主意。
    • XSLT 的 & 代码是什么?我在翻译中尝试过 & 但抛出错误?
    • &amp;amp; 是与符号的 XML 转义序列。不是 XSLT 或 XPath 需要它,而是 XML 本身需要它。
    • 由于某种原因这不起作用: translate(title, ' &():/\','')" &amp 导致了问题?
    • 除非您发布您尝试使用的完整代码行,否则我将无法判断出了什么问题。 ;-)
    【解决方案2】:

    你已经走了大部分路了:

    翻译('abcd', 'cbda', 'CBDA')

    会给出“ABCD”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 2021-10-10
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多