tags:

views:

149

answers:

1

I want to use XSL to remove some elements from a tree.

Suppose I have the following XML tree:

<?xml version="1.0" ?>
<mydoc>
    <file>
        <colors>
            <blue />
            <red />
            <green />
        </colors>
        <secret>
            <username />
            <password />
        </secret>
    </file>
</mydoc>

I want to remove the username and password nodes from it. How would I proceed with XSL ?

+3  A: 

You want an identity transform. A common design pattern in XSLT is a transform that will copy everything. Then you add templates to remove or transform what is different between the source and the target.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="username|password"/> <!-- this empty template will remove them -->
</xsl:stylesheet>
lavinio
Awesome, and very simply illustrated! +1
Cerebrus
+1 I didn't know about this pattern. Very clear and elegant.
mkoeller