Kirk Strobeck
Kirk Strobeck

Reputation: 18619

XSL: Strip HTML and truncate

I want to run this

<!-- This will remove the tag -->
<xsl:template name="remove-html">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&lt;')">
            <xsl:value-of select="normalize-space(substring-before($text, '&lt;'))"/>
            <xsl:text> </xsl:text>
            <xsl:call-template name="remove-html">
                <xsl:with-param name="text" select="normalize-space(substring-after($text, '&gt;'))"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="normalize-space($text)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

and this

<xsl:choose>
    <xsl:when test="string-length(header) > 22">
        <xsl:value-of select="substring(header, 0, 22)" />
        <xsl:text>&#8230;</xsl:text>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="header" />
    </xsl:otherwise>
</xsl:choose>

together .. how can I do that?

Upvotes: 0

Views: 2324

Answers (2)

Tom Howard
Tom Howard

Reputation: 6687

<xsl:variable name="stripped">
    <xsl:call-template name="remove-html">
        <xsl:with-param name="text" select="???"/>
    </xsl:call-template>
</xsl:variable>
<xsl:choose>
    <xsl:when test="string-length($stripped) > 22">
        <xsl:value-of select="substring($stripped, 0, 22)" />
        <xsl:text>&#8230;</xsl:text>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$stripped" />
    </xsl:otherwise>
</xsl:choose>

Replace ??? with the appropriate node

Upvotes: 2

ssamuel
ssamuel

Reputation: 427

Wrap your second choose in a named template, then replace your value-of in the first with a call-template to it.

Upvotes: 0

Related Questions