We have faced issues many times in XSLT when we require just to show text and no html content. So here is the solution. Call the template "remove-html" with passing the content and this will do it.
<!-- Calling the template that removes tag -->
<xsl:call-template name="remove-html">
<xsl:with-param name="text" select="{HtmlBody}"/>
</xsl:call-template>
<!-- This will remove the tag -->
<xsl:template name="remove-html">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="remove-html">
<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>
Thanks. It helped me.
ReplyDeleteThank you. This is really useful.
ReplyDeleteI'm still having difficulty removing HTML elements with attributes. Do you have any hints for this?
e.g.
foobar
I am not getting your question.
DeleteI have a @Description item , I want to remove all the tags before I display it ...... finding difficult to apply the remove tage template........ Any Help............ Mohan
ReplyDeleteSo where does the top part go? in the ContentQueryMain.xsl or somewhere else? The bottom section obviously goes int the ItemStyle.xsl. Thanks.
ReplyDeleteIt should work in ItemStyle.xsl.
ReplyDeleteThank you for the post! It seems this will help as I am trying to rollup the latest discussion board entries from a variety of project sites using a CQWP; however, I am a self-admitted neophyte to XML development/maintenance and would appreciate a little help.
ReplyDeleteI have the CQWP Group Style set to 'Default' and the Item Style set to 'Title and Description'. In SP Designer, I opened the ItemStyle.xsl and pasted the call code posted here immediately below the "div class="description"" start tag nested in the Default template. I then pasted the code starting with "xsl:template name="remove-html"" below the Default template's end tag (not nested). I save and refresh the CQWP page and the HTML markup is still displayed. (Note the tag delimiters have been replaced with double quotes).
Is there something obvious that I'm missing? Any guidance is greatly appreciated!
Here's how I did it:
DeleteAfter adding the template as you did, I found the section for the Item Style I am using, the one named "No Image" (I selected 'Title and Description in the CQWP presentation properties). In the template, I copied the DisplayTitle variable section, named the new variable DisplayDescription, changed the call-template to "remove-html", and replaced the two parameter lines with one named "text" and passed it "@Description". Finally, in the description div at the bottom of the NoImage template, I changed the xsl:value-of select to $DisplayDescription (our new variable).
Hope that helps!
Jordan
Thank you for the post.
ReplyDeleteThe template is useful, but the html format get lost.
What if I want to keep the format and transform it?
For example: " <b>this text should be bold</b>
I mean that was the goal just to remove the html. from the input but you can again concat strong tag.
DeleteIs there a way to strip an html tag, it's sub tags, and all text between them?
ReplyDeleteRemove the header tag and everything in between
Paul,
DeleteYou can do anything using XSL. This was just an example for starters.
Thanks,
Maulik Dhorajia
I tried to extend this template to keep superscript and subscript i.e remove all markups except sup and sub. But when I tried I am running into StackOverflowException, can you please guide with some functions/technique to acheieve this.
ReplyDeleteCouldn't you just use myHtmlNode/text() ?
ReplyDeleteWhen we give enhanced rich text the above template is not working
ReplyDeleteTHis is perfect for my needs, but how could i use this to remove only "p" and "/p" tags?
ReplyDeleteIS it possible?
Is it possible to use this solution to only remove certain tags?
ReplyDeleteI would like to remove all starting and ending paragraph tags.
Thank you, this is helpful.
ReplyDeleteHow could you return, for instance, the first 200 characters of a cleaned up string?
ReplyDelete