Re: [xsl] How to strip off all empty elements from a XML doc?

Subject: Re: [xsl] How to strip off all empty elements from a XML doc?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 07 Oct 2009 09:40:57 -0400
At 2009-10-07 15:39 +0200, Ben Stover wrote:
Just another question:

Assume a XML doc contains (sub)elements with no content like

<myelem22></myelem22>
or
<myelem22/>

How can I strip off/delete recursively ALL these empty elements
from the XML element tree (but keep all others)?

Add to a stylesheet with the identity template a template that adds nothing to the result tree for elements with no content:


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

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


I hope this helps.


. . . . . . . . . . Ken

--
Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes
in Copenhagen Denmark and Washington DC USA, October/November 2009
Interested in other classes?  http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread