RE: [xsl] Delete XML Node

Subject: RE: [xsl] Delete XML Node
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 31 Oct 2002 16:43:48 -0500

I've been somewhat reluctant to respond to your post, though it's easy enough, since it's actually hard to determine what the best answer is.

The simple answer is "wrap the output for your <Y> in a simple xsl:if to test whether your condition for outputting it is met". So your code might be:

<xsl:for-each select="Y">
  <xsl:if test="*"><!-- tests whether any element children of a Y exist -->

But I rather think, looking at your input and desired output, that there are better solutions. Whether they are actually better depend on the exact purposes of your transform, as well as what range of inputs it has to handle, etc. So, for example, if your input is


and your stylesheet is tasked with the job of *copying the input except Y elements that have no children*, this can easily be accomplished with a modified "identity stylesheet" (a stylesheet that copies the source tree to the result), like so:

<xsl:stylesheet version="1.0"

<xsl:template match="*">
  <!-- the default template for an element simply copies that element -->

<xsl:template match="Y[not(*)]"/>
<!-- except Y elements with no element children are removed -->


This will be both more robust, and easier to maintain and extend, than the "pull" approach you are taking using the for-each instructions.

If you don't understand how this is working, you want to do a bit of research on templates, apply-templates and the XSLT processing model, including the effect of the built-in templates.

Good luck,

At 04:03 PM 10/31/2002, you wrote:
Whoops, I shot myself in the foot here....

sorry folks I am new to XSL.... and pardon my ignorrance

To clarify things, I want an XSL which would output a <Y> if and only if it has a sub element in this case a <Z>. In the current example I have only one sub element <Z> but there can be more than one sub-element of <Y>.



I do not want <Y></Y> to appear in the output if all the sub-elements do not map.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

XSL-List info and archive:

Current Thread