Tuesday, May 8, 2012

Display content from rich text box using Xslt template

usage:

<xsl:call-template name="GetBodyNoTags">
                    <xsl:with-param name="origMsg" select="@Description" ></xsl:with-param>
                    <xsl:with-param name="bodyLength" select="100"></xsl:with-param>
                </xsl:call-template>

Implementation:

<xsl:template name="GetBodyNoTags">
      <xsl:param name="origMsg" />
      <xsl:param name="bodyLength" />
    <xsl:choose>
        <xsl:when test="contains($origMsg, '&lt;')">
            <xsl:variable name="test" select="concat(substring-before($origMsg, '&lt;'), substring-after($origMsg, '&gt;'))"></xsl:variable>
            <xsl:call-template name="GetBodyNoTags">
                <xsl:with-param name="origMsg" select="$test"></xsl:with-param>
                <xsl:with-param name="bodyLength" select="$bodyLength"></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="string-length($origMsg) &gt; $bodyLength">
                    <xsl:value-of select="concat(substring($origMsg, 0, $bodyLength), '...')" disable-output-escaping="yes"></xsl:value-of>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$origMsg" disable-output-escaping="yes"></xsl:value-of>
                </xsl:otherwise>
            </xsl:choose>                                  
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

1 comment: