Link to home
Start Free TrialLog in
Avatar of ethar turky
ethar turkyFlag for Saudi Arabia

asked on

remove html tag from xml elment

Hi,
How remove any tag from xml element in XSL.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

If this prefers to your previous question...

Do you mean that you want to strip out all the escaped html tags?
well, that is a bit tricky, it requires writing some sort of XML parser in XSLT1

consider this

<p class=">abc">foo<bar</p>

pretty tricky to figure out whet is a tag in that string

I recommend that you have a second pass (new XSLT that removes the tags from inside a <p> element (and lets hope the html inside is wellformed)

Can you use XSLT2?
Avatar of ethar turky

ASKER

lets say can we remove string start from  &lt;br /&gt;&lt;a   till end of element?
if you can specify what you need to remove, this is feasable

eg.
<br />
<a href="some-uri">foo</a>
...

would you need the foo to remain in <a> ?
I need to remove From "<a"   to the end

Original:
description:re ayasd cdn specifgdfy wsdft you nes to resdve, sdfeasdble<a href="some-uri">foo</a>asfda asfgag



will be
description:re ayasd cdn specifgdfy wsdft you nes to resdve, sdfeasdble
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks this is wot I need.
The br needs to be removed recursively since it can be more than one

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:atom="http://www.w3.org/2005/Atom"
    version="1.0">
    
    <xsl:template match="/rss">
        <html>
            <head/>
            <body>
                <xsl:apply-templates select="channel"/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="channel">
        <h1><a href="{link}"><xsl:value-of select="title"/></a></h1>
        <xsl:apply-templates select="item[position() &lt;= 5]"/>
    </xsl:template>
    
    <xsl:template match="item">
        <h3><a href="{link}"><xsl:value-of select="title"/></a></h3>
        <p><xsl:value-of select="pubDate"/></p>
        <p><xsl:apply-templates select="description"/></p>
        <xsl:if test="enclosure[contains(@type, 'image')]">
            <img src="{enclosure/url}"/>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="description">
        <xsl:variable name="pre-link-str">
            <xsl:choose>
                <xsl:when test="contains(., '&lt;a')">
                    <xsl:value-of select="substring-before(., '&lt;a')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="remove-br">
            <xsl:with-param name="str" select="$pre-link-str"/>
        </xsl:call-template>
        
    </xsl:template>
    
    <xsl:template name="remove-br">
        <xsl:param name="str"/>
        <xsl:choose>
            <xsl:when test="contains($str, '&lt;br')">
                <xsl:value-of select="substring-before($str, '&lt;br')"/>
                <xsl:variable name="remainder-str" select="substring-after($str, '&lt;br')"/>
                <xsl:choose>
                    <xsl:when test="starts-with(normalize-space($remainder-str), '/&gt;')">
                        <xsl:call-template name="remove-br">
                            <xsl:with-param name="str" select="substring-after($remainder-str, '/&gt;')"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="remove-br">
                            <xsl:with-param name="str" select="substring-after(substring-after($remainder-str, '&lt;/'), '&gt;')"/>
                        </xsl:call-template>
                     </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window