Re: [xsl] defaulting empty node to zero

Subject: Re: [xsl] defaulting empty node to zero
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Fri, 24 May 2002 22:34:48 +0200
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



Current Thread