Remove HTML markup in Content Query Web Part

When showing a multi line or text column with Rich text or Enhanced rich text all the supporting HTML tags will be visible in the result. The tags make the result unreadable for end users.

With XSLT it is possible to remove the tags and make the result clean and readable. All the markups will be removed, also text markups.


1. Open the ItemStyle.xsl in SharePoint designer.
2. Make a the new removeMarkUp template.

<xsl:template name="removeMarkup">
   <xsl:param name="string" />
   <xsl:when test="contains($string, '&lt;')">
      <xsl:variable name="nextString">
	<xsl:call-template name="removeMarkup">
	   <xsl:with-param name="string" select="substring-after($string, '&gt;')" />
         <xsl:value-of select="concat(substring-before($string, '&lt;'), $nextString)" />
      <xsl:value-of select="$string" />

3. Add the cleanBody variable to the template which renders the content query.

<xsl:variable name="cleanBody">
   <xsl:call-template name="removeMarkup">
      <xsl:with-param name="string" select="@Body"/>

4. Show the clean body with this code.

<xsl:value-of select="$cleanBody" />

5. All the HTML markup is removed!


22 Replies to “Remove HTML markup in Content Query Web Part”

  1. Hello All,

    I tried with the above mentioned codes in my CQWP but unfortunately this code is not reflecting any changes to the HTML display.I pasted the “removeMarkup” template at the end where the last <xsl:template tag end .Then I pasted the in the template that is rendering the CQWP and then tried by putting it in the calling template and the template that is to be called but to no avail. Can anyone please suggest me what more I can do to display a CQWP without markups.

    Amiya Patra

  2. Hi Amiya,

    I do not really understand what is going wrong in your situation. The solution works in 3 steps. 1. Make the removeMarkUp template. 2. Set the CleanBody variable in the Template you are using for the CQWP. 3 Call the Variable by using .

  3. I am equally confused. I put the contents of step 2 as the last template in the ItemStyle.xsl. That step is clear and understandable. For step 3, how can you determine the template used for the CQWP? For step 4, where do you call the variable?

  4. Hi Rob,

    The XSTL file consists of a number of templates. You can either change an existing one or add a new one. If you use a existing template you will need to figure out which one you are using. Open the setting for the CQWP and find the setting Item style under Styles. The name selected in this box is the template that is used.
    You will need to add the code from step 3 into that template to remove the markup. And add the code from step 4 to show the information.

    1. I am excited! I opened the picklist for Item Style and found removeMarkup at the bottom of the list. I still have absolutely no clue as to what the name of the field is where I place .

      1. Hi Rob,

        The removeMarkup is a bit of code that will change the display of a field. For example the field description or body field.The code will remove the HTML markup.

        To make this work you need to do the following.

        1. Create the removeMarkup template (step 2 in the blog)
        2. Create or change an existing template to show the field(s) (step 3/4 in the blog)

        Step 3 has a few lines of code. In the 4 line you see:

        You need to change the @Body into the field name that you want to be displayed without the HTML tags.

        Select the change or created template (not removeMarkup) in the cqwp.

      2. Thanks, Ben. Still couldn’t get it to work. I just went into the ItemStyle.xsl and used
        It is now able to render the HTML without displaying the tags.

  5. Hey Ben,

    I’ve been using this on a client’s Office 365 environment. It was fine with SP2010. However, now they’ve done an underwater upgrade (the sites themselves haven’t been upgraded yet) and suddenly the CQWPs were all throwing errors. I traced it to this template – as soon as it was called, the errors came up.

    When I replaced it with this, it all went well. Not quite sure exactly what the difference is, but maybe good to know.

    1. Hi There,

      Would it be possible be remove all HTML tags and leave behind tag.
      I want my final cleaned code to look like : plain text

  6. Hi Ben,

    Thank you for the post. I think it will help in my endeavor to display the latest posts on several discussion boards in a CQWP sans markup. I am just learning how to develop and maintain XML code and could use some assistance as I’m not able to remove the markup using the code posted above. Most likely user error. Here’s where I’m at:

    The CQWP’s Group Style is set to ‘Default’ and the Item Style is set to ‘Title and Description’. In SP Designer, I open the ItemStyle.xsl file. I paste the first section of code (step 2) that defines the removeMarkup template below the ‘Default’ template end tag (unnested). I paste the cleanbody variable code (step 3) nested in the Default template (same level as the other variables). I paste the call code (step 4) nested under the ‘div class=”description”‘ tag.

    Any ideas? I greatly appreciate any guidance as this has been a bugger.

  7. Thanks, Ben. The result is the CQWP presents the HTML markup along with the desired content, just as if the code was not inserted. Does the call need to be placed in a specific location? Would it be helpful to see my code?

  8. Hi There,

    Would it be possible be remove all HTML tags and leave behind tag.
    I want my final cleaned code to look like : plain text

    1. the above post removed image tag… i want the code to remove all html tags leaving behind image tag

  9. With this code that is not possible. You will need more complex code to do this. THe code needs to remove all tags but not image. Perhaps you need to build a if statement or some other trick.

  10. Didn’t know people were having so much trouble with this one issue.
    For me I resolve the same issue by adding ‘disable-output-escaping=”yes”‘ attribute to xsl:value-of element.
    For example, I typically use NoImage template and under … I add new display field call Body.

    Then CQWP would output the content correctly.

  11. After applying the template output is displaying as , for eg.(test output as <p> test </p>) i just want to display only the text. Any help would be appreciated.

  12. I know this is an older post, but I have a question: the code you suggest works when I’m logged in as an administrator. When I log in as a regular user and refresh, the HTML comes back, and some customizations I made in the XSL (as an admin) are no longer showing. Thoughts?

  13. I can see why this might be useful in scenarios where one wishes to have rich content stripped of all HTML mark-up, but I don’t see why you would not simply use in your XSL stylesheet to simply render the field content as rich-formatted text in the browser?

Leave a Reply

Your email address will not be published. Required fields are marked *