Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have several attributes which contain numbers. But one of them or several (or even every) may not be defined (i.e. no such attr in xml-file). I need to display sum of attrs, so when at least one of them is undefined I get NaN.

So how can I avoid getting NaN? I mean I need to interpret NaN value as 0, like NaN + 10 + 20 + NaN + 30 should give me 60 as result, but not NaN Is it any way to avoid NaN result without conditions, just like var||0 in JS will return 0 if var is undefined?

What I have tried:

Example of xml element. I need to sum every attr (attr3 is undefined)
<element attr1 = "5" attr2 = "1"/>

Example of xsl sum
<xsl:value-of select="@attr1 + @attr2 + @attr3"/>
Posted
Updated 27-Apr-16 16:53pm
Comments
Patrice T 27-Apr-16 16:05pm    
Why don't you check if attributes exists ?
Sergey Alexandrovich Kryukov 27-Apr-16 17:42pm    
The situation is actually much worse than that. I tried to describe it in my answer. :-)
—SA

No. Don't do it. NaN is not 0, 0 is not NaN. Pretending one is another would mean loss of data. Use NaN; this is a legitimate object you just fail to understand; it is very important.

I hope you understand that NaN is "not a number". By interpreting 0 as not a number, you put yourself in the position of ancient and early Medieval people who did not have zero or did not consider it as a number. Welcome back to the barbaric world!
(However, this roughly matches the level of understanding of mathematics and arithmetic by the U.S. Internal Revenue Service, people who collect taxes.
If you are one of them, your will probably need a century or two, to get to the world of mathematics a bit closer to the time of European Renaissance. :-))

Perhaps you need some understanding:
NaN — Wikipedia, the free encyclopedia[^],
Floating point — Wikipedia, the free encyclopedia[^],
IEEE floating point — Wikipedia, the free encyclopedia[^].

—SA
 
Share this answer
 
v3
Comments
[no name] 28-Apr-16 16:02pm    
5 also here
Sergey Alexandrovich Kryukov 28-Apr-16 17:06pm    
Thank you.
—SA
select="sum(@attr1 | @attr2 | @attr3)"
 
Share this answer
 
I know this question already has an accepted answer, but it always makes me cringe when I see hard coded solutions like this.

Better to to try to sum up all existing attributes, than to use their specific names.
What if you want to add attr4 and attr5 later?
Then you have to change your code to work for that scenario.

The solution below only works if you want to get the sum of all existing attributes.

Example XML
XML
<root>
    <element attr1="10" attr2="20" />
    <element attr1="10" attr2="20" attr3="30" />
    <element attr1="10" attr2="20" attr3="30" attr4="40" attr5="50" />
</root>


XSLT
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="text" indent="no" />

  <xsl:template match="root">
    <xsl:apply-templates select="element" />
  </xsl:template>

  <xsl:template match="element">
    Attribute Sum = <xsl:value-of select="sum(@*)" />
  </xsl:template>

</xsl:stylesheet>


Output
Attribute Sum = 30
Attribute Sum = 60
Attribute Sum = 150
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900