xslt 1.0 如何在选择条件下用 0(零)替换空值或空白值

问题描述:

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

如果 nodevalue 为空或空白,我想用 0(零)替换该值.问题是在这个计算中,如果任何节点值为空或空白,它会给出 NaN 结果.例如选择10-2+5-2- -4"

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"

试试看:

<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')"/>

编辑

请注意,以上只是一个可爱的技巧",旨在避免执行此操作的正确且直接的解决方案的冗长:

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.