Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes

Subject: Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 08 Nov 2010 14:03:56 -0500
At 2010-11-08 19:22 +0100, Hermann Stamm-Wilbrandt wrote:
I was given an identity transformation that eliminates empty nodes.
  <!-- identity template -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*[not(node())]" />

I was asked on how to do this until all empty nodes are gone, even the
newly created empty nodes after removal of an inner empty node.
The question was on whether this can be done without applying the
stylesheet again and again.
...
$ cat x.xml
<a><b>c<c/></b><b><c/></b></a>

Are you defining, then, an "ultimately" empty node as being a node that doesn't have descendent text nodes?


If not, then please clarify with more examples. The code below gives you what your recursive solution gives you, but I can't tell from your specification if that is what you really want.

Perhaps you are simply looking too deep at the problem?

I hope this helps.

. . . . . . . . . Ken

~/t/ftemp $ cat hermann.xml
<a><b>c<c/></b><b><c/></b></a>
~/t/ftemp $ xslt hermann.xml hermann.xsl
<?xml version="1.0" encoding="utf-8"?><a><b>c</b></a>
~/t/ftemp $ cat hermann.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="*[not(.//text())]"/>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
~/t/ftemp $



--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread