Strip specific html tag in xslt

csaladores's Avatar

csaladores

08 Dec, 2010 03:01 PM

Hi,

Is there a way to strip specific html tag using xslt and still keep html content?

I.E. <p><a href="http://www.google.com&quot;&gt;Google&lt;/a&gt;&lt;/p>

to

<p>Google</p>

  1. 2 Posted by Kris on 08 Dec, 2010 03:16 PM

    Kris's Avatar

    I would try something like this. This is some code used to remove all HTML tags that I modified to remove only the tag specified in the parameter from the html passed into it. This is untested but should at least get you started. You will need to call this twice, once passing "a" as the tag and again with "/a" as the tag to remove the start and end tags so there may be a better way to do this in one call to the template.

      <xsl:template name="removeTag">
          <xsl:param name="html"/>
          <xsl:param name="tag"/>
          <xsl:choose>
              <xsl:when test="contains($html, concat('&lt;',$tag))">
                  <xsl:value-of select="substring-before($html, concat('&lt;',$tag))"/>
                  <xsl:call-template name="removeTag">
                      <xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
                  </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:value-of select="$html"/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:template>
    

    Hope that helps get you on the path.
    Kris

  2. 3 Posted by csaladores on 08 Dec, 2010 03:23 PM

    csaladores's Avatar

    Thanks Kris for the reply. It's definitely a good start.

    Also using xsl:value-of removes all tags.

    I have to modify value-of to copy-of to preserve the html.

    I'll keep chugging along. I just need to remove <a> tag.

  3. 4 Posted by Ross on 04 Mar, 2011 09:06 PM

    Ross's Avatar

    Christian, do you still need help with this?

  4. Tim closed this discussion on 21 Jun, 2011 03:35 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac