[xsl] Recursively removing empty tags from deepest child upwards.

Subject: [xsl] Recursively removing empty tags from deepest child upwards.
From: David Holden <dh@xxxxxxxx>
Date: Mon, 22 Mar 2004 13:36:48 +0000
Hello,

 I'm trying to remove certain empty tags from a set a files

e.g., simplified example

<?xml version="1.0" encoding="US-ASCII"?>
<bdy>
   <section><p>not empty</p><p></p></section>
   <section><section><p></p></section></section>
</bdy>


I want

<?xml version="1.0" encoding="US-ASCII"?>
<bdy>
   <section><p>not empty</p></section>
</bdy>


i.e. in this example in the first section the empty <p></p> is removed and the 
whole of the second section removed since it contains just an empty p and 
empty section elements.


So far I have



<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:xalan="http://xml.apache.org/xslt";
 exclude-result-prefixes="xalan"
 version="1.0"
 >
  <!-- output format xml -->
  <xsl:output
   method="xml"
   encoding="US-ASCII">

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:apply-templates mode="copy"/>
  </xsl:template>

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

  <xsl:template match="//section|//p" mode="copy">
    <xsl:choose>
      <xsl:when test="normalize-space(.)">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" mode="copy"/>
        </xsl:copy>
      </xsl:when>
      <xsl:when test="count(./*)">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" mode="copy"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


But this only removes the deepest empty child element.


giving 

<?xml version="1.0" encoding="US-ASCII"?>
<bdy><section><p>not empty</p></section><section><section/></section></bdy>



I need to be able to recurse so that after removing the deepest child if then 
checks to seem if its parent is empty etc...



anyone help,

 thanks.

 Dave.




-- 
Dr. David Holden. (Systems Developer)
Crystallography Journals Online: <http://journals.iucr.org>

Thanks in advance:-
Please avoid sending me Word or PowerPoint attachments.
See: <http://www.fsf.org/philosophy/no-word-attachments.html>

UK Privacy (R.I.P)  : http://www.stand.org.uk/commentary.php3
Public GPG key available on request.
-------------------------------------------------------------

Current Thread