Stripping HTML tags when using XSLT

Search

Accessible SharePoint WebSites
Download ARF

Stripping HTML tags when using XSLT

http://blog.thekid.me.uk

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.

Posted by Vincent Rothwell on Thursday, 17 May 2007 12:49  - 16 Comments
Orininally printed from http://thekid.me.uk - Copyright Vincent Rothwell 2007
 

Comments

Sunday, 27 Jul 2008 03:36 by brian lemon
genius

Sunday, 27 Jul 2008 03:36 by mita
many thanks!

Sunday, 27 Jul 2008 03:37 by MoLo
This is exactly what I've been searching for. Thanks!

Thursday, 7 Aug 2008 07:27 by Steve M.
Excellent work. Saved me a ton of time! Many thanks to you.

Monday, 8 Sep 2008 05:53 by Boris
thhaaannnnkkk yoooouuuuu! :)

Tuesday, 16 Sep 2008 07:55 by Andre
many thanks :D

Thursday, 6 Nov 2008 12:02 by Etienne
You're a star mate, clean, nice, beautiful, efficient... fantastic! Many thanks

Tuesday, 6 Jan 2009 06:37 by mamta
really good.. and very neat.. appreciate ur way of coding.

Monday, 9 Feb 2009 01:19 by Neel
the link is very good. it is quite helpful

Wednesday, 11 Mar 2009 08:27 by Amazing piece of Code
Thanks

Monday, 8 Jun 2009 07:13 by Tim
very usefull. Thanks a bunch.

Tuesday, 14 Jul 2009 03:31 by Vips
Thanks. You are a champ. I was trying to solve this for many days, but not working. This is really helpful

Monday, 12 Oct 2009 03:19 by Nils
Excellent! One can't help to wonder why there isnt a function inbuilt for this...

Friday, 30 Oct 2009 07:44 by davetiye
güzel davetiye sözleri ve davetiye metinleri

Monday, 2 Nov 2009 06:59 by davetiye
güzel davetiye sözleri ve davetgiye metinleri

Wednesday, 9 Dec 2009 12:58 by
Typical pour explanation though!



Url

Email

Comments