[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] splitting a string at ,


Subject: RE: [xsl] splitting a string at ,
From: Ryan Graham <Ryan.Graham@xxxxxxxxxxxxx>
Date: Wed, 14 Jan 2004 16:46:17 -0700

>I have a string seperated with commas. I am trying to
>split the string at commas.
>
>How can this be done.

I know this is not the most elegant solution, but it works:

Given this source XML-

<doc>
	<foo>split,this,string,please</foo>
</doc>

This stylesheet snippet...

<xsl:template match="foo">
	<xsl:call-template name="splitString">
		<xsl:with-param name="string1" select="."/>
	</xsl:call-template>
</xsl:template>
	
<xsl:template name="splitString">
	<xsl:param name="string1"/>
	<xsl:choose>
		<xsl:when test="contains($string1,',')">
			<xsl:variable name="beforeComma"
select="substring-before($string1,',')"/>
			<xsl:element name="entry"><xsl:value-of
select="$beforeComma"/></xsl:element>
			<xsl:variable name="afterComma"
select="substring-after($string1,',')"/>
			<xsl:call-template name="splitString">
				<xsl:with-param name="string1"
select="$afterComma" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:element name="entry">
				<xsl:value-of select="$string1"/>
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

...produces the following output:

<entry>split</entry>
<entry>this</entry>
<entry>string</entry>
<entry>please</entry>


Of course you can modify this to suit your personal requirements.

HTH,
RG


>Thanks
>Archana

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread