You may often need to remove HTML tags from some random text block and just display the raw text content.
For example you may want to just display the text content of a multi-line rich text value using XSL.
But since a multi-line rich text value is internally formatted with html, the html tags will be presented as part of the output.
I have faced this situation many times in SharePoint.
As an example, the output you desire might be:
Note: This is a dummy notice.
But instead of the above, you might get something like:
Note: <div>This is a <em>dummy</em> notice.</div>
The method described here is a general technique that you can always use to remove HTML tags from any XSL value.
Add the following XSL template to the end of your stylesheet file:
<xsl:template name="strip-tags"> <xsl:param name="text"/> <xsl:choose> <xsl:when test="contains($text, '<')"> <xsl:value-of select="substring-before($text, '<')"/> <xsl:call-template name="strip-tags"> <xsl:with-param name="text" select="substring-after($text, '>')"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template>
Now let’s say the value you need to call is called @Note
Instead of calling it the usual way like this:
<xsl:value-of select="@Note" />
Call it like this instead:
<xsl:call-template name="strip-tags"> <xsl:with-param name="text" select="@Note"/> </xsl:call-template>
The HTML tags will be removed.
nice artikel, thank you for share…
This is a nice solution. What about tags embedded in tags? For instance, what if the value of the Note attribute were “This is a bold, italic note“. In this case, I think your approach would output
"This is a bold, italic note"
.Interesting point you brought up there. While I think the code I posted can deal with the scenario you mentioned, I have never tried it so I can’t be certain. If you or anyone else reading this tries it out, I’d like to know the results for sure!
Thanks for your comment.
Thank you for this article, realy helped me alot.
For me there was an extra requirement, the output needed to be chopped at 200 characters.
If you got this or an other requirement, just place the output in a variable and use this afterwards
Thanks for your comment, Vincent.
Great to know this helped.
Regards,