开发者

XSLT - Add space after lowercase followed by Uppercase letter

开发者 https://www.devze.com 2022-12-08 18:02 出处:网络
I have a simple string say: NiceWeather I\'d like to insert a space between \'e\' and \'W\' to produce:

I have a simple string say:

NiceWeather

I'd like to insert a space between 'e' and 'W' to produce:

Nice Weather

Is there any function I could use (XSLT 1.0) to put t开发者_JAVA百科he space in?


Here is a more direct answer to this question. bastianneu's answer definitely puts you on the right track, but if you want a template that specifically breaks CamelCase strings into individual words, this will do it for you.

<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>


What you are looking for is often called "String Split".

Look at this useful example:

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

Small Template:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消