You have to call one template(replacing "breaks") and apply another template(putting "hrefs") on the result of the first call.
I changed some points in your code and added one main template, called "convert". Since both jobs(replacing line-breaks and putting hrefs) have many things in common, I wrote the main template, which accepts a parameter "handler-name" and selects appropriate "job" to perform.
I generalized the problem: now the source XML doc can contain any content, not just "plain text". It is more interesting, of course :)
Even in the case of "plain text", we have to use "RTF to nodeset" conversion; explicitly or implicitly.
Why? Because when you want to replace line-breaks and then put hrefs, the input of the second "operation" will contain "br" elements; when you want to put hrefs and then replace line-breaks, you'll have an "a" elements in the input to the second operation. So, we have to use "RTF to nodeset" conversion even in the simplest case of "plain-text".
I used Saxon 6.5.2. You just have to specify "version='1.1'" in the XML declaration, or explicitly use exslt:node-set() function. I have chosen the first approach.
Here is the code:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="convert">
<xsl:with-param name="tree-fragment">
<xsl:call-template name="convert">
<xsl:with-param name="tree-fragment" select="."/>
<xsl:with-param name="handler-name" select="'breaks'"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="handler-name" select="'hrefs'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="convert">
<xsl:param name="tree-fragment"/>
<xsl:param name="handler-name"/>
<xsl:for-each select="$tree-fragment/child::node()">
<xsl:choose>
<xsl:when test="current()[self::processing-instruction() or self::comment()]">
<xsl:copy-of select="current()"/>
</xsl:when>
<xsl:when test="current()[self::text()]">
<xsl:choose>
<xsl:when test="$handler-name = 'hrefs'">
<xsl:call-template name="put-hrefs">
<xsl:with-param name="text" select="current()"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$handler-name = 'breaks'">
<xsl:call-template name="put-breaks">
<xsl:with-param name="text" select="current()"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:when test="current()[self::*]">
<xsl:copy>
<xsl:for-each select="current()/@*">
<xsl:attribute name="{name(current())}">
<xsl:value-of select="current()"/>
</xsl:attribute>
</xsl:for-each>
<xsl:call-template name="convert">
<xsl:with-param name="tree-fragment" select="current()"/>
<xsl:with-param name="handler-name" select="$handler-name"/>
</xsl:call-template>
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="put-hrefs">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, 'http:')">
<xsl:variable name="textstart" select="substring-before($text,'http:')"/>
<xsl:variable name="rest" select="concat(substring-after($text,'http:'),'')" />
<xsl:variable name="link" select="concat('http:',substring-before($rest,' '))" />
<xsl:text>#160;</xsl:text>
<xsl:variable name="textend" select="substring-after($rest,' ')" />
<xsl:value-of select="$textstart" /><a><xsl:attribute name="href"><xsl:value-of select="$link" /></xsl:attribute><xsl:value-of select="$link" /></a>#160;
<xsl:call-template name="put-hrefs">
<xsl:with-param name="text" select="normalize-space($textend)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="put-breaks">
<xsl:param name="text"/>
<xsl:param name="from" select="'#x0A;'" />
<xsl:param name="to"><br /><br /></xsl:param>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:value-of select="substring-before($text, $from)" />
<xsl:copy-of select="$to" />
<xsl:call-template name="put-breaks">
<xsl:with-param name="text" select="substring-after($text, $from)" />
<xsl:with-param name="from" select="$from" />
<xsl:with-param name="to" select="$to" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
If you'll see some #xc2 symbols(I guess the processor outputs them) in IE, change the encoding to UTF8.