XSLT - 在小写后跟大写字母后添加空格

时间:2009-10-14 14:15:55

标签: xslt

我有一个简单的字符串说:

NiceWeather

我想在'e'和'W'之间插入一个空格来产生:

Nice Weather

我可以使用任何函数(XSLT 1.0)来放置空间吗?

2 个答案:

答案 0 :(得分:6)

以下是这个问题的更直接的答案。 bastianneu的答案肯定会让你走上正轨,但是如果你想要一个专门将CamelCase字符串分解为单个单词的模板,这将为你做到。

<xsl:template name="breakIntoWords">
  <xsl:param name="string" />
  <xsl:choose>
    <xsl:when test="string-length($string) &lt; 2">
      <xsl:value-of select="$string" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="breakIntoWordsHelper">
        <xsl:with-param name="string" select="$string" />
        <xsl:with-param name="token" select="substring($string, 1, 1)" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="breakIntoWordsHelper">
  <xsl:param name="string" select="''" />
  <xsl:param name="token" select="''" />
  <xsl:choose>
    <xsl:when test="string-length($string) = 0" />
    <xsl:when test="string-length($token) = 0" />
    <xsl:when test="string-length($string) = string-length($token)">
      <xsl:value-of select="$token" />
    </xsl:when>
    <xsl:when test="contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring($string, string-length($token) + 1, 1))">
      <xsl:value-of select="concat($token, ' ')" />
      <xsl:call-template name="breakIntoWordsHelper">
        <xsl:with-param name="string" select="substring-after($string, $token)" />
        <xsl:with-param name="token" select="substring($string, string-length($token), 1)" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="breakIntoWordsHelper">
        <xsl:with-param name="string" select="$string" />
        <xsl:with-param name="token" select="substring($string, 1, string-length($token) + 1)" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

答案 1 :(得分:1)

您正在寻找的通常称为“字符串拆分”。

看看这个有用的例子:

http://www.abbeyworkshop.com/howto/xslt/xslt-split-values/index.html

小模板:

http://www.exslt.org/str/functions/split/str.split.template.xsl