Hyperlink
Convert plain text URL strings into clickable HTML hyperlinks.
Description
Super simple way (read: rudimentary) to convert plain text strings that contain http:// or https:// URLs into clickable HTML hyperlinks, expanded on code originally from this thread - http://www.stylusstudio.com/xsllist/200305/post81170.html
Could probably do with some Regex instead of matching "http://" or "https://", but its was not necessary for my use.
XSLT
View Raw
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Converts any number of plain text URL strings into HTML formatted hyperlinks in a passage of text --> <!-- Made by @george_wilson January 2013, updated December 2013 --> <xsl:template name="hyperlink"> <xsl:param name="string" select="string()" /> <xsl:variable name="http"> <xsl:choose> <xsl:when test="contains($string, 'http://')"> <xsl:text>http://</xsl:text> </xsl:when><xsl:when test="contains($string, 'https://')"> <xsl:text>https://</xsl:text> </xsl:when><xsl:otherwise> <xsl:text>false</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test="$http = 'false'"> <!-- No links, output string --> <xsl:value-of select="$string"/> </xsl:when><xsl:otherwise> <!-- Links detected, replace them --> <!-- Set up variables --> <xsl:variable name="before" select="substring-before($string, $http)" /> <xsl:variable name="after" select="substring-after($string, $http)" /> <xsl:variable name="url" select="concat($http, substring-before($after,' '))"/> <xsl:variable name="rest" select="substring-after($string, $url)" /> <!-- Output the text --> <xsl:value-of select="$before"/> <xsl:choose> <!-- If the url is at then end, $rest doesn't work --> <xsl:when test="substring-after($url,$http) != ''"> <a href="{$url}" rel="nofollow"> <xsl:value-of select="$url" /> </a> <!-- Check the remainder for links --> <xsl:call-template name="hyperlink"> <xsl:with-param name="string" select="$rest"/> </xsl:call-template> </xsl:when><xsl:otherwise> <a href="{$url}{$after}" rel="nofollow"> <xsl:value-of select="$after" /> </a> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>