Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two values in xslt that sometimes can be null

Tags:

xml

xslt

xslt-1.0

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

like image 303
Joe Avatar asked Mar 21 '23 16:03

Joe


1 Answers

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.

like image 140
michael.hor257k Avatar answered Apr 01 '23 15:04

michael.hor257k