Add two values in xslt that sometimes can be null

xml, xslt, xslt-1.0

Solution

if "number(/fields/field[@name='value2'])" would produce a NaN it should be the number 0

You could try something like this:

<xsl:decimal-format name="coerce" NaN="0" />
...
<xsl:variable name="a" select="format-number(/fields/field[@name='value1'], '#', 'coerce')"/>
<xsl:variable name="b" select="format-number(/fields/field[@name='value2'], '#', 'coerce')"/>
...
<xsl:value-of select="$a + $b"/>

Note: the formatting used in the example assumes integer input.

Problem

I would like to add to values from a xml to another one using xslt. I am using xml version 1. ``` <xsl:value-of select="number(/fields/field[@name='value1'])+number(/fields/field[@name='value2'])"/> ``` How could I do this if value1 or value2 sometimes would be empty and produce and NaN? I know that I can use if and when to see if value1 or value2 is not empty but let's say I can't check that. How could I solve this? What I would like to do is if "number(/fields/field[@name='value2'])" would produce a NaN it should be the number 0 then it would work. Best regards Joe

Original source