[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Recursively removing empty tags from deepest child

Subject: Re: Recursively removing empty tags from deepest child upwards.
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 22 Mar 2004 13:53:37 -0800 (PST)
xslt remove empty nodes
It seems that by "empty" you mean an element, which does not have any
text-node children.

If this is the case, then the following simple override of the identity
transformation solves the problem:


<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*" />

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

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

</xsl:stylesheet>


When applied on your original source.xml:

<bdy>
   <section><p>not empty</p><p></p></section>
   <section><section><p></p></section></section>
</bdy>

the above transformation produces the wanted result:

<bdy><section><p>not empty</p></section></bdy>


Hope this helped.

Cheers,

Dimitre Novatchev,
FXSL developer,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html



"David Holden" <dh@xxxxxxxx> wrote in message
news:200403221336.48500.dh@xxxxxxxxxxx
> 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.
> -------------------------------------------------------------
>



__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.