Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 3rd, 2007, 06:16 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default remove empty elements but not ones with attributes

I'm trying to strip empty elements and empty attributes from a document but leave in elements that are empty but have attributes that are populated. So far I've got the following code which removes all empty elements even if they have attributes populated. How do I add a conditions so something like <employee id="60"/> does not get stripped?

Thanks,
Dan


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

Reply With Quote
  #2 (permalink)  
Old March 3rd, 2007, 07:02 PM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

I tend to tackle this kind of problem by applying templates only to elements and not to attributes (no real reason, just personal preference). That makes it:

<xsl:template match="*">
  <xsl:copy>
  <xsl:copy-of select="@*[.!='']"/>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[.=''][not(@*[.!=''])]"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Elements (EMPTY) and Attributes #Required Ian ORourke XML 10 July 14th, 2016 11:07 PM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 09:50 AM
Elements and attributes Morrislgn XSLT 1 June 20th, 2005 12:13 PM
Help:Recursive Function - attributes into elements spencer.clark XSLT 1 June 10th, 2005 08:56 AM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 07:34 AM



All times are GMT -4. The time now is 10:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.