?
Solved

remove html tag from xml elment

Posted on 2013-05-29
8
Medium Priority
?
451 Views
Last Modified: 2013-05-30
Hi,
How remove any tag from xml element in XSL.
0
Comment
Question by:ethar1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39205936
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?
0
 

Author Comment

by:ethar1
ID: 39205961
lets say can we remove string start from  &lt;br /&gt;&lt;a   till end of element?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39205974
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> ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ethar1
ID: 39205988
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
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 39206003
that is easy

so you need substring-before(., '&lt;a')

and from that you need the <br /> removed?
0
 

Author Comment

by:ethar1
ID: 39206027
thanks this is wot I need.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39206384
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

0
 

Author Comment

by:ethar1
ID: 39207115
Thanks,
another question , please response.
http://www.experts-exchange.com/Hardware/Components/Q_28142696.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So, if your PC is old or new and it does not boot or has no display then what do you do? Precautions : -During connecting or disconnecting devices, be sure to have the AC electrical power disconnected -Temporarily ground yourself, or use a wris…
Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question