Stripping HTML Tags From an XSL Value (in SharePoint 2010)

I had a scenario where the contents of the value I was looking for was a multiline rich text value.  The contents I cared about displaying to the end user was simply the text nodes contained within this value.  In other words, what I cared about seeing was this:

Important Information: Exact timing and details of this meeting to be confirmed.   This meeting will be held in Nanaimo

But, what I was actually seeing on the screen was this:

Important Information: <div><div><div>Exact timing and details of this meeting to be confirmed.</div> <div> </div> <div>This meeting will be held in Nanaimo</div></div> </div>

The value in the Row of my XSL result looked basically like this (with all the other node attributes stripped out for brevity’s sake).

[sourcecode language=”XML”]
<Row ImportantInformation="&lt;div class=&quot;ExternalClass731DB49F9ACC43AAA73AB0C68C117F2F&quot;&gt;&lt;div&gt;&lt;div&gt;Exact timing and
details of this meeting to be confirmed.&lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;div&gt;This meeting will be held in
Nanaimo&lt;/div&gt;&lt;/div&gt; &lt;/div&gt;" />
[/sourcecode]

I’m not going to go into the details of how to get the XSL setup (basically, I changed /Style Library/XSL Style Sheets/ItemStyle.xsl file in SharePoint Designer 2010), but to convert this value from the unwanted HTML output to the (desirable) text output, you can simply run it through the following transform.

I added this stylesheet template to the end of the file (before the close </xsl:stylesheet>).

[sourcecode language=”XML”]
<xsl:template name="strip-tags">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, ‘&lt;’)">
<xsl:value-of select="substring-before($text, ‘&lt;’)"/>
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="substring-after($text, ‘&gt;’)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
[/sourcecode]

To call this, I added a call to the XSL style sheet by changing my code

FROM:

[sourcecode language=”XML”]
<div class="description">
Important Information:<xsl:value-of select="@ImportantInformation" />
</div>
[/sourcecode]

TO:

[sourcecode language=”XML”]
<div class="description">
Important Information:
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="@ImportantInformation"/>
</xsl:call-template>
</div>
[/sourcecode]

Hopefully this can help other people strip the HTML from a multi-line text value.

6 responses to “Stripping HTML Tags From an XSL Value (in SharePoint 2010)

  1. Hi, This is exactly what I’m looking for, but I’m having some trouble implementing it. I have a content query webpart which queries a list in another site within the same site collection. I am returning the column “description” within the webpart, but this is showing html tags.

    Following your instructions, I made the following changes to strip the html tags:

    In my web part, under the section “Fields to Display”, I have added “Description” to the “Title” value. All other fields in this section I have left blank (Ie. Link, Image, item).

    I updated the ItemStyle.xslt as follows:

    Changed the description div in (xsl:template name=”Default”)
    From:

    To:

    Source looks as follows:

    Test List

    <p>Here is my test item.</p>

    Any advice you could offer would be greatly appreciated.

    Thanks
    sara

  2. I’ll try again, this time preserving the html tags :)….

    Hi, This is exactly what I’m looking for, but I’m having some trouble implementing it. I have a content query webpart which queries a list in another site within the same site collection. I am returning the column “description” within the webpart, but this is showing html tags.

    Following your instructions, I made the following changes to strip the html tags:

    In my web part, under the section “Fields to Display”, I have added “Description” to the “Title” value. All other fields in this section I have left blank (Ie. Link, Image, item).

    I updated the ItemStyle.xslt as follows:

    Changed the description div in (xsl:template name=”Default”)
    From:
    <div class=”description”>
    <xsl:value-of select=”@Description” />
    </div>
    To:
    <div class=”description”>
    <xsl:call-template name=”remove-html”>
    <xsl:with-param name=”text” select=”@Description”/>
    </xsl:call-template>
    </div>

    Source looks as follows:

    <span title=”Test List – Displays a dynamic view of content from your site.” id=”WebPartTitleWPQ3″ class=”js-webpart-titleCell”>
    <h2 style=”text-align:justify;” class=”ms-webpart-titleText”><nobr><span>Test List</span><span id=”WebPartCaptionWPQ3″></span></nobr></h2></span>
    </div><div WebPartID=”8ed2d2a2-14cf-4671-b4c3-e1209d150e16″ WebPartID2=”c90f4385-40eb-4840-aaf7-f1a0f72484f7″ HasPers=”false” id=”WebPartWPQ3″ width=”100%” class=”ms-WPBody noindex ms-wpContentDivSpace ” allowRemove=”false” allowDelete=”false” style=”” ><div id=”cbqwpctl00_ctl40_g_c90f4385_40eb_4840_aaf7_f1a0f72484f7″ class=”cbq-layout-main”><ul class=”dfwp-column dfwp-list” style=”width:100%” ><li class=”dfwp-item”><div class=”item”><div class=”link-item”><a href=”/_layouts/15/CopyUtil.aspx?Use=id&Action=dispform&ItemId=2898&ListId=63fe8a6f-ec86-419b-b8e5-07b1a9fa57ed&WebId=5c877997-4d1e-404c-b519-0971add8b7a6&SiteId=abda19d1-b4f7-4492-8bd2-13c2dea18b44&Source=https%3A%2F%2Fmydomain%2Esharepoint%2Ecom%2FSitePages%2FHome%2Easpx” title=””><p>This is my test item.</p></a><div class=”description”></div></div></div></li>

    Any advice you could offer would be greatly appreciated.

    Thanks
    Sara

  3. Sara,

    Did you add the XSL template for “strip-tags” to your XSL file as I describe in the article? I notice you seem to have renamed the call to it to be “remove-html” – the template name would have to match that, so both the template name and the call to the template would both need to be the same name. Does that make sense to you?

Comments are closed.