View Single Post
  #3 (permalink)  
Old August 2nd, 2009, 10:02 AM
rjonk rjonk is offline
Authorized User
Points: 204, Level: 4
Points: 204, Level: 4 Points: 204, Level: 4 Points: 204, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up This is my solution that works for me now

Description:
This template truncates the text of the introductie node until a maximum characters are reached. When the text is truncated
an ellipsis is added to the truncated text. When the text is smaller than the maximum no ellipsis is added
at the end of the introductie text a link will be added

Parameters:
* limit (optional) number of characters to limit by. Default 250 characters
* suffix (optional) the suffix character string to use when the text is truncated. Default '...'
* url the url on the 'more' link
Example:
<xsl:apply-templates select="path/to/your/introductie" mode="truncate"/>

Code:
   <xsl:template match="art:introductie" mode="truncateTree">
        <xsl:param name="limit" select="250"/>
        <xsl:param name="suffix" select="'...'"/>
        <xsl:param name="url"/>
        
        <!-- copy introduction node to redesign the content and later process this copy-->
        <xsl:variable name="truncatedNode">
            <xsl:copy-of select="*"/>
        </xsl:variable>
        
        <!-- create new truncated node -->
        <xsl:variable name="truncatedNodeTree">
            <xsl:apply-templates mode="truncate" select="$truncatedNode/*">
                <xsl:with-param name="limit" select="$limit"/>
                <xsl:with-param name="suffix" select="$suffix"/>
                <xsl:with-param name="url" select="$url"/>
            </xsl:apply-templates>
            
        </xsl:variable>
        
        <!--process new created truncated introduction tree with the general xslt-->
        <xsl:apply-templates select="$truncatedNodeTree/*"/>
        
    </xsl:template>
    
    <!-- process each node -->
    <xsl:template match="*" mode="truncate">
        <xsl:param name="limit"/>
        <xsl:param name="suffix"/>
        <xsl:param name="url"/>
       
        <xsl:variable name="preceding-strings">
            <xsl:copy-of select="preceding::text()"/>
        </xsl:variable>
        
        <!-- precedingchar: number of characters up to the current node -->
        <xsl:variable name="precedingchar" select="string-length(normalize-space($preceding-strings))"/>
        
        <xsl:if test="$precedingchar &lt; $limit">
            <xsl:element name="{name()}">
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates mode="truncate">
                    <xsl:with-param name="limit" select="$limit"/>
                    <xsl:with-param name="suffix" select="$suffix"/>
                </xsl:apply-templates>
                <!-- only add link at end of first element this is the first node inside introductie-->
                <xsl:if test="position()=1">
                    <xsl:text> </xsl:text><link class="actionLink" xlink:href="{$url}" xlink:title="More">More</link>
                </xsl:if>
            </xsl:element>
        </xsl:if>
    </xsl:template>
    
    <!-- process each text node -->
    <xsl:template match="text()" mode="truncate">
        <xsl:param name="limit"/>
        <xsl:param name="suffix"/>
        <xsl:variable name="preceding-strings">
            <xsl:copy-of select="preceding::text()"/>
        </xsl:variable>
        
        <!-- precedingchar: number of characters up to the current node -->
        <xsl:variable name="precedingchar" select="string-length(normalize-space($preceding-strings))"/>
        
        <xsl:if test="$precedingchar &lt; $limit">
           <!-- totalchar: number of characters including current node -->
           <xsl:variable name="totalchar" select="$precedingchar + string-length(.)"/>
           
           <xsl:choose>
               <xsl:when test="$totalchar &gt; $limit ">
                   <!-- truncate until limit reached -->
                   <xsl:value-of select="substring(., 1, ($limit - $precedingchar))"/>
                   <xsl:value-of select="$suffix"/>
                   <xsl:text> </xsl:text>
               </xsl:when>
               <xsl:otherwise>
                   <!-- dont have to truncate text -->
                   <xsl:value-of select="."/>
               </xsl:otherwise>
           </xsl:choose>
        </xsl:if>
        
    </xsl:template>
this is an example xml
Code:
<Article xml:lang="nl" xmlns="http://article/version_1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
	<titel>Test truncation</titel>
	<article.info>
		<date>9-7-2009</date>
		<introductie>
			<alinea>This is the introduction text
				<special>This must be created bold</special>
				This is some text node too. <special >Make this also bold please</special> Again a  text node. For more information please visit:
				<special>www.frontendplace.nl</special>
				This is the last textnode.
			</alinea>
		</introductie>
	</article.info>
</Article>
and this is the template inside the calling xslt
Code:
<xsl:template name="summary">
		<xsl:param name="url" select="'.'"/>
		<xsl:param name="truncate" select="200"/>
		<h3>
			<a href="{$url}">
				<xsl:value-of select="art:titel"/>
			</a>
		</h3>
		<div class="introductie">
			<xsl:apply-templates mode="truncateTree" select="art:artikel.info/art:introductie">
				<xsl:with-param name="limit" select="$truncate"/>
				<xsl:with-param name="suffix" select="'...'"/>
				<xsl:with-param name="url" select="$url"/>
			</xsl:apply-templates>
		</div>
	</xsl:template>
	<xsl:template match="art:special">
		<strong><xsl:apply-templates/></strong>
	</xsl:template>
Reply With Quote