• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » Backend (Server-Side) » Remove HTML Tags From An XSL Value

Remove HTML Tags From An XSL Value

By Ehi Kioya 5 Comments

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, '&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>

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.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), XML Tagged With: XML, XSL, XSLT

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Comments

  1. Abdul Jalil says

    July 21, 2014 at 11:24 am

    nice artikel, thank you for share…

    Reply
  2. Jesse Alama says

    April 24, 2015 at 2:35 am

    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".

    Reply
    • Ehi Kioya says

      April 24, 2015 at 4:46 am

      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.

      Reply
  3. Vincent van der Laan says

    June 13, 2018 at 8:52 am

    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

    Reply
    • Ehi Kioya says

      June 13, 2018 at 12:16 pm

      Thanks for your comment, Vincent.

      Great to know this helped.

      Regards,

      Reply

Leave a Reply Cancel reply

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

Primary Sidebar

23,736
Followers
Follow
30,000
Connections
Connect
14,568
Page Fans
Like
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • How To Change A SharePoint List Or Library URL How To Change A SharePoint List Or Library URL
    All versions of the SharePoint user interface provide an option to change the title (or display name) of a list or library. Changing SharePoint library URL (or internal name), however, is not exactly very intuitive. We will discuss the process in this article.
  • About
  • Contact

© 2022   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy