[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: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Mon, 22 Mar 2004 08:29:45 -0700
xslt remove empty tags
This XSL...

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

<xsl:template match="/">
	<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*">
	<xsl:if test=". != ''">
		<xsl:copy>
			<xsl:element name="name()">
				<xsl:copy-of select="@*"/>
				<xsl:apply-templates/>
			</xsl:element>
		</xsl:copy>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>

Turns this XML...

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

Into this...

<?xml version="1.0" encoding="UTF-8"?>
<bdy>
   <section name="foo"><p>not empty</p></section>   
</bdy>

Notice the slight modification of the source to show that you need to
add the <xsl:copy-of select="@*"/> to the xsl to ensure that you copy
the attributes of those tags that evaluate to true in the <xsl:if...>
tag.

Best of luck!

<M:D/>

-----Original Message-----
From: David Holden [mailto:dh@xxxxxxxx] 
Sent: Monday, March 22, 2004 6:37 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Recursively removing empty tags from deepest child
upwards.

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

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.