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
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

Use this:

<xsl:template match="*[not(child::node())]"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top