Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt 1.0 how to replace empty or blank value with 0 (zero) in select condition

Tags:

xslt

xslt-1.0

<xsl:call-template name="SetNetTemplate">
<xsl:with-param name="xyz" select="$node1value
                                 + $node2value
                                 + $node3value
                                 - $node4value
                                 - $node5value
                                 - $node6value"/>
</xsl:call-template>

If nodevalue is empty or blank, i want to replace that value with 0 (zero). Problem is that in this calculation if any nodevalue is empty or blank, it is giving NaN result. e.g. select "10-2+5-2- -4"

like image 652
ironman Avatar asked Dec 07 '22 03:12

ironman


1 Answers

Try it this way:

<xsl:with-param name="xyz" select="translate(number($node1value), 'aN', '0')
                                 + translate(number($node2value), 'aN', '0')
                                 + translate(number($node3value), 'aN', '0')
                                   ...
                                 - translate(number($node6value), 'aN', '0')"/>

EDIT

Note that the above is just a "cute trick" designed to avoid the verbosity of the proper and straightforward solution that would do this:

<xsl:choose>
    <xsl:when test="number($node1value)">
        <xsl:value-of select="$node1value" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="0" />
    </xsl:otherwise>
</xsl:choose>

to each and every one of your operands before trying to treat them as numbers.

like image 151
michael.hor257k Avatar answered Apr 22 '23 13:04

michael.hor257k