xsl-list
[Top] [All Lists]

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

2010-11-09 07:07:28
I will post the requirements (which I do not know right now) as soon
as I can contact my colleague tomorrow morning German time.
The requirements were really simple.
No mixed data, no comments, but linebreaks:
...
  <a>
     <b/>
     <c/>
  </a>
...

So "*[not(normalize-space())]" was enough.
Thanks for all the responses.

$ cat t.xsl
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

  <xsl:output method="xml" version="1.0" encoding="UTF-8"
    indent="no" omit-xml-declaration="no" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

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


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
Fixpack team lead
WebSphere DataPower SOA Appliances


From:       Hermann Stamm-Wilbrandt/Germany/IBM(_at_)IBMDE
To:         xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Date:       11/08/2010 08:50 PM
Subject:    Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes



Thanks Ken,

I will post the requirements (which I do not know right now) as soon
as I can contact my colleague tomorrow morning German time.


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:       "G. Ken Holman" <gkholman(_at_)CraneSoftwrights(_dot_)com>
To:         xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Date:       11/08/2010 08:29 PM
Subject:    Re: [xsl] [XSLT 1.0] Q: recursively eliminate empty nodes



At 2010-11-08 20:15 +0100, Hermann Stamm-Wilbrandt wrote:
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:

Of course they would differ ... you haven't described the
requirements and we volunteers are just guessing.

$ 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.

Good ... it will stop the guessing.

For now the XSLT 1.0 solution below will check for empty elements
with your new stipulation.

I hope this helps.

. . . . . . Ken

~/t/ftemp $ cat hermann2.xml
<a><b>c<c/></b><b><c/><!--test--></b></a>
~/t/ftemp $ xslt hermann2.xml hermann.xsl
<?xml version="1.0"
encoding="utf-8"?><a><b>c</b><b><!--test--></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() | .//comment() |
                            .//processing-instruction())]"/>

<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(_at_)CraneSoftwrights(_dot_)com
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--