Xslt 1.0 how to replace an empty or empty value with 0 (zero) in the conditions of choice

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

If the nodevalue is empty or empty, I want to replace this value with 0 (zero). The problem is that with this calculation, if any node value is empty or empty, it gives the result NaN. e.g. select "10-2 + ​​5-2 -4"

+4
source share
2 answers

Try it like this:

<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 a correct and simple 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>

for each of your operands before trying to treat them as numbers.

+7
source

Try

<xsl:with-param name="xyz" select="concat(0, $node1value)
                                 + concat(0, $node2value)
                                 + concat(0, $node3value)..."/>

etc.

+3
source

Source: https://habr.com/ru/post/1528772/


All Articles