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

Re: [XSLT 1.0] Q: recursively eliminate empty nodes

Subject: Re: [XSLT 1.0] Q: recursively eliminate empty nodes
From: Andriy Gerasika <andriy.gerasika@xxxxxxxxx>
Date: Mon, 08 Nov 2010 20:54:32 +0200
Re:  [XSLT 1.0] Q: recursively eliminate empty nodes
On 11/08/2010 08:22 PM, Hermann Stamm-Wilbrandt wrote:

Hello,


I was given an identity transformation that eliminates empty nodes.
   <!-- identity template -->
   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()" />
     </xsl:copy>
   </xsl:template>
   <xsl:template match="*[not(node())]" />


identity template must have priority=-9


I was asked on how to do this until all empty nodes are gone, even the
newly created empty nodes after removal of an inner empty node.
The question was on whether this can be done without applying the
stylesheet again and again.

I came up with below (working) "XSLT 1.0 + exslt:node-set()" solution.

The question is, whether this can be done with pure XSLT 1.0?
Especially I want to know whether it can be done without node-set()
function -- just to make sure that not every problem looks like a
nail given the exslt:node-set() hammer ...


(x.xsl one-time elimination, y.xsl recursive elimination) http://stamm-wilbrandt.de/en/xsl-list/y.xsl

$ cat x.xml
<a><b>c<c/></b><b><c/></b></a>
$
$ xsltproc x.xsl x.xml
<?xml version="1.0" encoding="UTF-8"?>
<a><b>c</b><b/></a>
$
$ xsltproc y.xsl x.xml
<?xml version="1.0"?>
<a><b>c</b></a>
$
$ cat y.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:exslt="http://exslt.org/common"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"


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

   <!-- recursive template -->
   <xsl:template name="elimininateEmptyNodes">
     <xsl:param name="elem"/>
     <xsl:choose>
       <xsl:when test="$elem//*[not(node())]">
         <xsl:variable name="aux">
           <xsl:apply-templates select="$elem/*"/>
         </xsl:variable>
         <xsl:call-template name="elimininateEmptyNodes">
           <xsl:with-param name="elem" select="exslt:node-set($aux)"/>
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:copy-of select="$elem"/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

   <xsl:template match="/">
     <xsl:call-template name="elimininateEmptyNodes">
       <xsl:with-param name="elem" select="/"/>
     </xsl:call-template>
   </xsl:template>

</xsl:stylesheet>
$


Mit besten Gruessen / Best wishes,


Hermann Stamm-Wilbrandt
Developer, XML Compiler, L3
Fixpack team lead
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research&  Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294

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.