Retrieving the last value of a delimited string in XSL

14 07 2011

I had to print the last value of a dynamic tilde (~) delimited string in the HTML (email content). The style sheets are done in XSL. This is how i made it work:

I defined a template:

	<xsl:template name="returnLastValueofString">
		<xsl:param name="inputString"/>
		<xsl:choose>
			<xsl:when test="substring-after($inputString,'~')">
				<xsl:call-template name="returnLastValueofString">
					<xsl:with-param name="inputString" select="substring-after($inputString,'~')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$inputString"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

This is how i called the value:

	<xsl:call-template name="returnLastValueofString">
		<xsl:with-param name="inputString" select="$OriginalStringAttribute"/>
	</xsl:call-template>
Advertisement

Actions

Information

2 responses

14 07 2011
shobhit

the template you have defined, is better in case you want to iterate upon each value of your delimited string, but in your use case you want to retrieve the last value from the delimiter.

So to save your processing time, you should write a reg ex which begins from “the end” of the string, goes back in the direction and search out for the first occurrence of your delimiter. This way you can have ur index of the last value of the delimited string directly and saving lot of un-necessary iterations.

14 07 2011
sakkisays

That works too. Thanks much.

I was sure that the string that i have is very short mostly. I haven’t looked in that perspective.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.