This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Looping over characters in a string
- To: Theodore Weatherly <tweather at slamdunknetworks dot com>
- Subject: Re: [xsl] Looping over characters in a string
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Thu, 10 May 2001 09:14:24 +0100
- CC: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <73EBD6E4A6FFD411BA2900B0D068464D74E4C8@exchangeserver.slamdunknetworks.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Theodore,
> Is it possible to loop over characters in a string?
Jarno's shown you two ways of doing it using an increasing index. The
third way is to use a recursive template that walks over the string,
removing the first character from the string with each recursion. For
example:
<xsl:template name="add-X">
<xsl:param name="string" select="." />
<xsl:if test="$string">
<xsl:text>X</xsl:text>
<xsl:call-template name="add-X">
<xsl:with-param name="string"
select="substring($string, 2)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
How exactly you use this template depends on where you are to start
with. For example, you could use it in the template matching the tag
element (the advantage of this is that it means you don't have to pass
a parameter to the template, as I've set the default for the $string
parameter to be the string value of the current node).
<xsl:template match="tag">
<xsl:call-template name="add-X" />
<xsl:value-of select="." />
</xsl:template>
No ASCII art I'm afraid. Hope that helps anyway,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list