This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: defaulting empty node to zero
- From: Joerg Heinicke <joerg dot heinicke at gmx dot de>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Fri, 24 May 2002 22:34:48 +0200
- Subject: Re: [xsl] defaulting empty node to zero
- References: <200205231749.NAA21319@biglist.com> <3CEE9C76.9010700@myrealbox.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Chuck Paussa wrote:
> I want to find the numeric value of a node. If the node is empty (e.g.:
> <taxAmount/>) I want zero. I also want zero when applying sum() to the
> same or a set of similar nodes.
> Is there a solution to this problem other than setting a variable, check
> the variable = NaN, return the variable or zero?
>
> Chuck
Hi Chuck,
you need another approach for this. Process the elements step by step:
XML:
<root>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test></test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
</root>
XSLT:
<xsl:template match="root">
<xsl:apply-templates select="test[1]"/>
</xsl:template>
<xsl:template match="test">
<xsl:param name="sum" select="0"/>
<xsl:variable name="newsum">
<xsl:choose>
<xsl:when test="number(.)">
<xsl:value-of select="$sum + ."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:apply-templates select="following-sibling::test[1]">
<xsl:with-param name="sum" select="$newsum"/>
</xsl:apply-templates>
<xsl:if test="not(following-sibling::test)">
<xsl:value-of select="$newsum"/>
</xsl:if>
</xsl:template>
Regards,
Joerg
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list