Dave Brondsema wrote:
> Note: the regex used in the 'spacify' template doesn't work.
> How can I make the
> replace() function work? Since it gives an error about the
> function not being
> found, I haven't tested the regex either: it's supposed to
> put a space in before
> capital letters (except for multiple sequential capital letters).
Hi Dave
I've attached a text file containing an xslt 1 template called "splitString"
which could either just replace the "spacify" template or could be called
from it:
<xsl:template name="spacify">
<xsl:param name="name" select="''"/>
<xsl:call-template name="splitString">
<xsl:with-param name="restOfString" select="$name"/>
</xsl:call-template>
</xsl:template>
Cheers
Con
<xsl:template name="splitString">
<xsl:param name="restOfString"/>
<xsl:variable name="uppercase">(ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="currentLetter" select="substring($restOfString,1,1)"/>
<xsl:choose>
<xsl:when test="contains($restOfString, '(') or
contains($restOfString,' ')">
<xsl:value-of select="$restOfString"/>
</xsl:when>
<xsl:when test="string-length($restOfString) >= 2">
<!-- there's a possibility it needs to be split -->
<xsl:choose>
<xsl:when test="contains($uppercase,$currentLetter)">
<xsl:variable name="followingLetter"
select="substring($restOfString,2,1)"/>
<xsl:if
test="not(contains($uppercase,$followingLetter))">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="$currentLetter"/>
<xsl:call-template name="splitString">
<xsl:with-param name="restOfString"
select="substring($restOfString,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- current letter is lower-case - just spit
it out -->
<xsl:value-of select="$currentLetter"/>
<xsl:call-template name="splitString">
<xsl:with-param name="restOfString"
select="substring($restOfString,2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- end of string - just write the remainder -->
<xsl:value-of select="$restOfString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>