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

Re: How to remove (in addition) empty attributes?

Subject: Re: How to remove (in addition) empty attributes?
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Thu, 3 Dec 2009 08:50:06 -0500
Re:  How to remove (in addition) empty attributes?
Ah, guess I was answering a slightly different question. If you are
trying to kill any element that has either no child nodes at all, or
has only child attribute nodes that are empty, perhaps

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output indent="yes"/>
   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>
   <xsl:template match="*[normalize-space( concat(.,@*) )='']"/>
 </xsl:stylesheet>

will do the trick.

The first part is a typical identity transform. The last template
there matches any element whose content & attribute values combined
consist of nothing other than whitespace.

There may be better ways to do this, but I think it does what you
want.

> The problem with this is that only elements OR attributes are
> stripped but not both at the same time.
> Assume the following XML source:
> <ns11:elem1 myattr="">
>    <ns22:subelem2 />
>    <ns22:subelem3></ns22:subelem3>
> </ns11:elem1>
> Then applying the XSLT script below will result in
> <ns11:elem1>
> </ns11:elem1>
> As you can see the ns11:elem1 persists.
> It is handled only in the remove attribute template and does not
> match the other remove-empty-element template.
> So how can I iteratively/recursively re-apply the script/templates
> on the result again?
> XSLT script:
> <?xml version="1.0"?>
> <xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
>   <xsl:output indent="yes"/>
>   <xsl:template match="@*[normalize-space(.)='']"/>
>   <xsl:template match="*[not(node())]"/> 
>   <xsl:template match="@* | node()">
>      <xsl:copy>
>        <xsl:apply-templates select="@* | node()"/>
>      </xsl:copy>
>   </xsl:template>
> </xsl:stylesheet>

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.