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

Subject: Re: [xsl] Recursively removing empty tags from deepest child upwards.
From: Mukul Gandhi <mukulgandhi2003@xxxxxxxxxxx>
Date: Mon, 22 Mar 2004 14:37:22 +0000 (GMT)
I slightly modified the XSL. This seems to work.

<?xml version="1.0" encoding="UTF-8"?>
<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="yes"/>

<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:if test="normalize-space(.)">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"
mode="copy"/>
        </xsl:copy>
    </xsl:if>
</xsl:template>    	
</xsl:stylesheet>

Regards,
Mukul

 --- David Holden <dh@xxxxxxxx> wrote: > 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.
>
-------------------------------------------------------------
>  

________________________________________________________________________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more. 
Go to: http://in.insurance.yahoo.com/licspecial/index.html

Current Thread