In XSLT, you usually donβt delete the elements you want to delete, but you copy the elements you want to keep:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://www.example.com/ns#" version="1.0"> <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> <xsl:template match="/ns:stuff"> <xsl:copy> <xsl:apply-templates select="ns:things"/> </xsl:copy> </xsl:template> <xsl:template match="ns:things"> <xsl:copy> <xsl:apply-templates select="ns:currency"/> <xsl:apply-templates select="ns:currency_code3"/> </xsl:copy> </xsl:template> <xsl:template match="ns:currency"> <xsl:copy-of select="."/> </xsl:template> <xsl:template match="ns:currency_code3"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet>
The example above copies only currency and currency_code3 . The output is as follows:
<?xml version="1.0" encoding="UTF-8"?> <ns:stuff xmlns:ns="http://www.example.com/ns#"> <ns:things> <ns:currency>somecurrency</ns:currency> <ns:currency_code3/> </ns:things> </ns:stuff>
Note. I have added a namespace declaration for your ns prefix.
If you want to copy everything except a few elements, you can see this answer
source share