tags:

views:

32

answers:

1

I need to rip some xml nodes from a xml document. The source is

<root>
    <customElement>
     <child1></child1>
     <child2></child2>
    </customElement>
    <child3></child3>
    <child4></child4>
</root>

the result should be

<root>
    <child1></child1>
    <child2></child2>
    <child3></child3>
    <child4></child4>
</root>

As you can see only the 'customElement' element is removed, but the child elements are still part of the result document. How can I do this using xslt transformation.

+2  A: 

Here's a simple solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

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

<!-- here we specify behavior for the node to be removed -->
<xsl:template match="customElement">
   <xsl:apply-templates select="@*|node()"/>
</xsl:template>

</xsl:stylesheet>
Jweede
+1. However, the `@*` is extraneous in the latter `apply-templates`. (With the example input, try deleting the whitespace between `<root>` and `<customElement>` and adding an attribute to `customElement` to see what happens: the attribute ends up in the `root` node in the output.)
Jukka Matilainen