Remove XML Node text using XSLT

936 views Asked by At

I need to modify xml document with XSLT. what I need is to remove some node name from xml document.

Example:

<link ref="www.facebook.com">
       <c type="Hyperlink">www.facebook.com<c>
</link>

I need to convert this xml as follows (remove the <c> node and attribute form xml),

<link ref="www.facebook.com">
       www.facebook.com
</link>

I tried to do this in many ways but none of worked out well. any suggestions how can I do this ?

2

There are 2 answers

0
michael.hor257k On BEST ANSWER

Here's one way:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="c">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Here's another:

<xsl:template match="link">
    <xsl:copy>
        <xsl:copy-of select="@* | c/text()"/>
    </xsl:copy>
</xsl:template>
0
Lech Rzedzicki On

And here's one more approach:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="link">
    <link>
    <xsl:copy-of select="@*"/><!-- copies the @ref attribute (and any other) -->    
    <xsl:value-of select="c[@type='hyperlink']"/>
    </link>    
</xsl:template>