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

孤街浪徒 提交于 2020-02-02 04:16:01

问题


<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"


回答1:


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.




回答2:


Try

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

etc




回答3:


For me this worked:

number(concat('0',$nodeValue))


来源:https://stackoverflow.com/questions/22018309/xslt-1-0-how-to-replace-empty-or-blank-value-with-0-zero-in-select-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!