user1307353
user1307353

Reputation: 21

Need to truncate output of template in XSLT

I am new to XSLT and want to truncate output(to 150 char, if beyond that) created by a template(below).

<!-- output the field description   -->
<xsl:template name="Desc">
    <xsl:value-of select="@description"/>
    <!-- append the description with the options for choiceFields   -->
    <xsl:if test="name() = 'choiceField'">
        <xsl:for-each select="./child::*">
            <xsl:value-of select="concat(@data,'=',@tag)"/>
            <xsl:if test="not(position() = last())">
                <xsl:text>;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:if>
</xsl:template>

Thanks in advance.

Upvotes: 2

Views: 569

Answers (1)

Abel
Abel

Reputation: 57169

Disclaimer: the original answer was by Martin Honnen, but he deleted his answer (no reason given) and the OP stated that the answer was actually correct. Hence I'll repeat it here for completeness, but if Martin undeletes his answer, I will remove this duplicate.


As it is a named template the template itself will not output anything, you need to call it. If you don't want its complete output then capture the result in a variable and use only 150 characters of that variable e.g.

<xsl:variable name="desc">
  <xsl:call-template name="Desc"/>
</xsl:variable>
<xsl:value-of select="substring($desc, 1, 150)"/>

Upvotes: 1

Related Questions