Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2009, 02:56 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Remove empty element

I have a few elements that I need to remove if they have no value. I want to specify the names (test the name) of the elements in order to remove them. I do not want an across the board sweep to remove all elements without values. How can I remove MyElement, below?

The element could be prefixed with the namespace or not, as follows:

Code:
<ns:MyElement/>

<MyElement/>
 
Old October 6th, 2009, 01:05 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
__________________
Rummy

Last edited by mrame; October 6th, 2009 at 01:07 AM..
 
Old October 6th, 2009, 04:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Write a stylesheet that uses an identity template to copy most nodes, and an empty template to delete the others:

Code:
<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="element-t0-be-deleted[not(child::node())]"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
fixit (October 6th, 2009)
 
Old October 6th, 2009, 04:54 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Remove element with certain value

Conversely, how can I remove an element that starts with a certain value, such as a letter 'N' ?

Thanks,
 
Old October 7th, 2009, 06:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Does the element name start with that letter? Or the contents of the element?
If is the contents of the element then do e.g.
Code:
<xsl:template match="*[starts-with(., 'N')]"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove empty cells and paste data from cell below aquavion Excel VBA 4 September 28th, 2009 09:52 AM
how to remove empty values in array thava Perl 3 August 20th, 2009 07:01 AM
xhtml, remove element Kabe XML 4 November 8th, 2007 12:09 PM
remove empty elements but not ones with attributes dupdup XSLT 1 March 3rd, 2007 07:02 PM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.