Removing empty nodes from XML using XSLT - Attributes should not be removed from non-empty nodes

StackOverflow https://stackoverflow.com/questions/23633541

  •  21-07-2023
  •  | 
  •  

Question

I am using below template to remove empty nodes from an XML, but it is also removing the class attribute from the non-empty nodes:

<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="*[not(child::node())]"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

I don't want to remove attributes from the non-empty nodes. Please suggest?

Was it helpful?

Solution

Use this:

<xsl:template match="*[not(child::node())]"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top