I am trying to transform a given XML using xslt. The caveat is that I would have to delete a parent node if a given child node is not present. I did do some template matchin
A different solution is to use the 'xsl:copy-of' element.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" />
<xsl:template match="Cars">
<xsl:copy>
<xsl:copy-of select="Car[Price]" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Super close. Just change your last template to:
<xsl:template match="Car[not(Price)]"/>
Also, it's not incorrect but you can combine your 2 xsl:output
elements:
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--Identity template to copy all content by default-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Car[not(Price)]"/>
</xsl:stylesheet>