xsl-list
[Top] [All Lists]

Formatting hyperlinks and paragraphs

2003-07-09 14:43:40
7/9/2003
5:39 PM

xsl-list,

In  a  perfect  world the stylesheet code below will take text from an
xml  element,  and:

1)  transform  text  that begins with 'http' into hyperlinks 2) format
the output to preserve line breaks.

At  the  moment  the code successfully formats the hyperlinks, but not
line breaks. Hopefully someone can point out my error?

Regards,

Pete

[code]
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
xmlns:msxsl="urn:schemas-microsoft-com:xslt"

<xsl:output method="html" version="4.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
                <xsl:apply-templates /> 
</xsl:template>



<!--Begin Sample Template  -->

<xsl:template match="Sample">   
<!-- hold the formatted text in this variable -->
<xsl:variable name="temp">
                        <xsl:call-template name="NewLine2Break" >
                                <xsl:with-param name="strTemp" 
select="Sample/text()" />
                        </xsl:call-template>
</xsl:variable>

<!-- transform the text held in the temp var by passing the var to this 
template call -->
<xsl:call-template name="text2hyper">
                <xsl:with-param name="strTemp" select="msxsl:node-set($temp)" />
        </xsl:call-template>    
</xsl:template>


<!--++++++++++++++HELPER TEMPLATES+++++++++++++++-->

<!-- TEMPLATE: convert text beginning with 'http:' to hyperlink -->
<xsl:template name="text2hyper">
        <xsl:param name="text" select="." />
        <xsl:choose>
                <xsl:when test="contains($text, 'http:')">
                        <xsl:variable name="textstart" 
select="substring-before($text,'http:')" /><!-- everything before token -->
                        <xsl:variable name="rest" 
select="concat(substring-after($text,'http:'),'  ' )" /><!-- everything after 
token-->
                        
                        <xsl:variable name="link" 
select="concat('http:',substring-before($rest,' '))" /><!--build the 
hyper-link-->
                        <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>
                                <xsl:text>&#160;</xsl:text>
                                <xsl:call-template name="text2hyper">
                                        <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>   
        <br />  
</xsl:template>

<!-- TEMPLATE: converts newline to <br> "break"-->
<xsl:template name="NewLine2Break">
   <xsl:param name="string" select="Sample/text()"/>
   <xsl:param name="from" select="'&#xA;'" />
   <xsl:param name="to">
      <br /><br />
   </xsl:param>
   <xsl:choose>
      <xsl:when test="contains($string, $from)">
         <xsl:value-of select="substring-before($string, $from)" />
         <xsl:copy-of select="$to" />
         <xsl:call-template name="NewLine2Break">
            <xsl:with-param name="string"
                            select="substring-after($string, $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="$string" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>

[/code]



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



<Prev in Thread] Current Thread [Next in Thread>