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

CLi*_*own 6 xslt

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

NiceWeather
Run Code Online (Sandbox Code Playgroud)

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

Nice Weather
Run Code Online (Sandbox Code Playgroud)

是否有任何我可以使用的功能(XSLT 1.0)来放置空间?

Tec*_*ium 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>
Run Code Online (Sandbox Code Playgroud)


bas*_*neu 1

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

看看这个有用的例子:

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

小模板:

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