Solved

remove html tag from xml elment

Posted on 2013-05-29
8
434 Views
Last Modified: 2013-05-30
Hi,
How remove any tag from xml element in XSL.
0
Comment
Question by:ethar1
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 500 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sound on server 13 91
power cord wear? 12 71
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 359
IIS components 2 17
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…
More and more people are using the enhanced small form-factor pluggable (SFP+) tranceivers, and speed is of utmost importance. Testing of speeds are critical to ensure that the devices will meet the speed requirements. There are some testing challen…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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