Re: [xsl] auto-hyperlinker (how to search/replace)?

Subject: Re: [xsl] auto-hyperlinker (how to search/replace)?
From: Terence Kearns <terencek@xxxxxxxxxxxxxxxxxxx>
Date: Tue, 03 Jun 2003 11:27:36 +1000
OK, that code works great after 1 minor modification :)

Here is the code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:param name="hyperLinkIndicator" select="'http://'" />

<xsl:template name="hyperlinker">
<xsl:param name="string" select="string()" />
<xsl:choose>
<xsl:when test="contains($string, $hyperLinkIndicator)">
<xsl:value-of select="substring-before($string, $hyperLinkIndicator)" />
<xsl:variable name="rest" select="substring-after($string, $hyperLinkIndicator)" />
<xsl:variable name="url" select="concat($hyperLinkIndicator, substring-before($rest, ' '))" />
<a href="{$url}">
<xsl:value-of select="$url" />
</a>
<xsl:call-template name="hyperlinker">
<xsl:with-param name="string" select="substring-after(concat($hyperLinkIndicator,$rest), $url)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


</xsl:stylesheet>

I moved the search text into a script param called hyperLinkIndicator

the only change I had to make to the original script was the select statement in the recursive call-template directive. It now reads
substring-after(concat($hyperLinkIndicator,$rest), $url)
instead of
substring-after($rest, $url)


A very useful script indeed which can be modified for other tokens.



It's worth mentioning that you shouldn't fall into the trap that I did.
I assigned the output of that script to a variable like so

<xsl:variable name="foo">
    <xsl:call-template name="hyperlinker">
        <xsl:with-param name="string" select="text()" />
    </xsl:call-template>
</xsl:variable>

and then proceeded to access $foo like so

<xsl:value-of select="$foo" />

needless to say, my hyperlinks never showed up until I used

<xsl:copy-of select="$foo" />



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


Current Thread