Re: [xsl] omitting empty elements when copying

Subject: Re: [xsl] omitting empty elements when copying
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 5 Feb 2003 12:53:26 +0000
Hi Jan,

> I want to copy only the elements with content

Whenever you want to copy-with-amendments, your best starting point is
the identity template, which performs a deep copy by recursing down
the tree:

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

Then you can add templates that match the things that you want to
remove from the tree and do nothing with them. In your case, a
template that matches the empty elements:

<xsl:template match="*[not(node())]" />

Note that "empty" means different things to different people. The
above will filter out elements that are truly empty, but won't filter
out elements that contain only comments, processing-instructions or
whitespace-only text. It also won't filter out elements that only
contain other elements that are empty. So you might want:

<xsl:template match="*[not(normalize-space())]" />

instead, which will filter out all elements that don't contain any
textual content.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread