Limit Characters (Read More...)

Topics: XSLT
Oct 20, 2011 at 2:29 PM

I think I remember seeing a post on who to limit characters when calling in a value of an xml field, but can't find it.  I want to add "Read More..." if the description field is longer than XX number of characters?  Is there a function that I can add to my xsl:value-of  call?

 

Thanks in advance.

Coordinator
Oct 20, 2011 at 2:45 PM
Edited Oct 20, 2011 at 2:46 PM

You can you some of build in xsl/xpath functions (http://www.w3schools.com/xpath/xpath_functions.asp). It would look something like that:

 

<xsl:choose>
	<xsl:when test="string-length(@Description) &gt; 50">
		<xsl:value-of select="substring(@Description, 1, 45)" /> ...
		<a href="....">Read more</a>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="@Description"/>
	</xsl:otherwise>
</xsl:choose>

 

Nov 6, 2011 at 9:36 PM

How can I incorporate substring with ParseXhtmlBodyFragment.  I want to limit the content length without loosing the HTML styles.  Any ideas?

 

This did not work:
<xsl:value-of select="substring(mp:ParseXhtmlBodyFragment(@Description), 1, 45)" /> ...
Coordinator
Nov 7, 2011 at 8:08 AM

Hello,

I don't know  how to limit the content length without loosing the HTML styles, but I recommend next solution.
For the details link I think it is enough to have Description content without  HTML tags....so firstly you remove HTML tags then limit the string, something like this:

 <xsl:template match="/">
    <html>
      <head>
      </head>
       <body>
        ....
        <p>
            <xsl:variable name="DescriptionText">
              <xsl:apply-templates select="@Description" mode="striptag" />
            </xsl:variable>
            <xsl:value-of select="substring($DescriptionText, 1, 45)" /> ...
        </p>
      </body>
    </html>
  </xsl:template>
<xsl:template mode="striptag" match="*">
    <xsl:param name="str" select="." />
    <xsl:choose>
      <xsl:when test="string-length(substring-after($str,'&lt;')) = 0">
        <xsl:value-of select="$str" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring-before($str,'&lt;')" />
        <xsl:if test="string-length(substring-after($str,'&gt;'))&gt;0">
          <xsl:apply-templates select="." mode="striptag">
            <xsl:with-param name="str" select="substring-after($str,'&gt;')" />
          </xsl:apply-templates>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>