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

Subject: Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Mon, 8 Nov 2010 20:15:28 +0100
Thanks David,

on the input file x.xml I posted your solutions produce the same output.

As Ken asked for more inputs I tried adding a comment and then
the output of your solutions and the recursive solution differ:

$ cat d.xml
<a><b>c<c/></b><b><c/><!--test--></b></a>
$
$ xsltproc d1.xsl d.xml
<?xml version="1.0" encoding="UTF-8"?>
<a><b>c</b></a>
$
$ xsltproc d2.xsl d.xml
<?xml version="1.0" encoding="UTF-8"?>
<a><b>c</b></a>
$
$ xsltproc y.xsl d.xml
<?xml version="1.0"?>
<a><b>c</b><b><!--test--></b></a>
$

I have to ask the originator of the problem for the exact requirements.


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
Fixpack team lead
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294



From:       David Carlisle <davidc@xxxxxxxxx>
To:         xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc:         Hermann Stamm-Wilbrandt/Germany/IBM@IBMDE
Date:       11/08/2010 07:56 PM
Subject:    Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes



On 08/11/2010 18:22, Hermann Stamm-Wilbrandt wrote:
>
> Hello,
>
> 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.
>
> I came up with below (working) "XSLT 1.0 + exslt:node-set()" solution.
>
> The question is, whether this can be done with pure XSLT 1.0?
> Especially I want to know whether it can be done without node-set()
> function -- just to make sure that not every problem looks like a
> nail given the exslt:node-set() hammer ...
>

it depends if this is all the stylesheet is doing or if there are other
templates processing other nodes. In the latter case you probably have
to two two passes, with :node-set but if it really is an identity
template plus strip empty nodes then really you are stripping any nodes
with text node descendants so (assuming you use xsl:strip-space
elements="*" to get rid of indentation you can just use

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

or


<xsl:template match="*[not(normalize-space())]" />


David

Current Thread