Stripping HTML tags when using XSLT

If you are using XML/XSLT to display a list of pages, and that list contains some HTML from within the page, it is sometimes good to remove the HTML markup from the text so that it displays correctly in the list.

The following XSL template shows how to remove the tags from the HTML so that you are only left with the text within the HTML.

<xsl:template name="removeHtmlTags">
    <xsl:param name="html"/>
    <xsl:choose>
        <xsl:when test="contains($html, '&lt;')">
            <xsl:value-of select="substring-before($html, '&lt;')"/>
            <!-- Recurse through HTML -->
            <xsl:call-template name="removeHtmlTags">
                <xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$html"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

To use the template you call it passing the HTML as a paramter...

<xsl:variable name="pureText">
    <xsl:call-template name="removeHtmlTags">
        <xsl:with-param name="html" select="Body" />
    </xsl:call-template>
</xsl:variable>

<div class="blogText">
    <xsl:value-of select="substring($pureText, 0, 250)" />...<br/>
</div>

This shows setting a variable called pureText and setting it to the result of calling the template removeHtmlTags. Finally it writes out the resultant text within a <DIV> tag.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值