Jump to content
Sign in to follow this  
Adam McCabe

FMSA CWP: Sum gives 'NaN'

Recommended Posts

This may be a n00b question posted elsewhere, but I couldn't seem to find the answer here.

When I use the 'sum' function in the XSL files produced by the FM Site Assistant, there is some code that checks if any of the fields is not a number. If so, it reports "N/A". Here's the code:




 

  

 

 

  

 







Here's the problem: If a record has no data in the field that is being summed, or if there is non-numerical data, such as some dashes, the sum function returns "N/A".



I've tried substituting the following for records that don't have numerical data in that field:




but that only affects the output, which the sum function doesn't care about.

Is there a way to program for this, or do I have to scrub the data in the database to be existing and numerical?

TIA.

Share this post


Link to post
Share on other sites

No, it's not trivial, and I had to look up the XSLT FAQ and to test it on an own example.

You can use these two lines only and forget everything else:






This looks very weird, but works. Here the explanation:

In XSLT, square brackets [] in an XPath define a test expression.

So the first square bracket pair just selects the field with field name (attribute name) stored in the variable $_summary-field-name-1 . Let's assume the field name is 'x'.

The second square bracket pair contains a number conversion of the value in the current XPath context (defined by the dot), i.e. the value just selected by the summing loop. The expression is true or 1 if the number conversion yields a number; if the number conversion yields 'NaN' it is false or 0 because NaN is the only number that isn't equal to itself.

Hence, if true, this evaluates to fmrs:field[@name = 'x'][1], the first element of field 'x' (and there is only one element!). If false, this evaluates to fmrs:field[@name = 'x'][0], which does not exist and hence is not considered by sum().

Edited by Guest

Share this post


Link to post
Share on other sites

Martin:

That is a most excellent and well-explained response. I will certainly be using the NaN does not equal NaN trick in the future.

Thanks so much! :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.