This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: empty elements
- From: "DuCharme, Bob (LNG)" <bob dot ducharme at lexisnexis dot com>
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 4 Dec 2001 15:49:37 -0500
- Subject: RE: [xsl] empty elements
- Reply-to: xsl-list at lists dot mulberrytech dot com
Polini Mirco wrote:
>I must delete my xml input from empty elements; i'm using xalan processor
>How can I control, in the stylesheet, if an element is empty.
If I understand you correctly, you want to check whether an element is empty
and not copy it to the result tree if so, right?
Something like this will do it:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:if test="normalize-space(.)">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The first template rule checks whether, after getting rid of extraneous
white space, an element has anything left. If so, it gets copied. (The other
template rule copies the attributes.)
There's more about this in section 3.10 of my book.
Bob DuCharme www.snee.com/bob <bob@
snee.com> see http://www.snee.com/bob/xsltquickly for
info on book "XSLT Quickly" from Manning Publications.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list