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.

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 ( It would look something like that:


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


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)" /> ...
Nov 7, 2011 at 8:08 AM


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 firstly you remove HTML tags then limit the string, something like this:

 <xsl:template match="/">
            <xsl:variable name="DescriptionText">
              <xsl:apply-templates select="@Description" mode="striptag" />
            <xsl:value-of select="substring($DescriptionText, 1, 45)" /> ...
<xsl:template mode="striptag" match="*">
    <xsl:param name="str" select="." />
      <xsl:when test="string-length(substring-after($str,'&lt;')) = 0">
        <xsl:value-of select="$str" />
        <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;')" />