Lucifer
Lucifer

Reputation: 842

XML Removing child node keeping value using xslt

I am trying to remove child node and keeping parent node and child node value, like this. My xml will look like

<parent>
<child>
<value>
123
</value>
</child>
</parent>

and output will look like

<parent>123</parent>

I need to parse using any xslt. Any help will appreciated.

Upvotes: 0

Views: 62

Answers (2)

Tim C
Tim C

Reputation: 70618

If you wanted to remove an element, and all its descendants, you would do this...

<xsl:template match="child" />

However, if you simply wanted to remove the element, but keep its descendants, you would do this...

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

Where <xsl:apply-templates /> is short for <xsl:apply-templates select="node()" />

Try this XSLT

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

    <xsl:template match="child|value">
        <xsl:apply-templates />
    </xsl:template>

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

Upvotes: 1

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

try this:

<xsl:template match="parent">
    <xsl:copy>
        <xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>

Upvotes: 1

Related Questions