@Dimitre Novatchev's answer is certainly correct and elegant, but there is a generalization (which the OP did not ask about): what if the element you want to filter also has children or text that you want to keep?
I believe this minor option covers this case:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="DropMe"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
A matching condition can be difficult to specify other attributes, etc., and you can use several such patterns if you drop other things.
So this entry:
<?xml version="1.0" encoding="UTF-8"?> <mydocument> <p>Here text to keep</p> <p><DropMe>Keep this text but not the element</DropMe>; and keep what follows.</p> <p><DropMe>Also keep this text and <b>this child element</b> too</DropMe>, along with what follows.</p> </mydocument>
produces this conclusion:
<?xml version="1.0" encoding="UTF-8"?><mydocument> <p>Here text to keep</p> <p>Keep this text but not the element; and keep what follows.</p> <p>Also keep this text and <b>this child element</b> too, along with what follows.</p> </mydocument>
Thanks to the XSLT Cookbook .
Sboisen Mar 08 '19 at 21:53 2019-03-08 21:53
source share