homepage Welcome to WebmasterWorld Guest from 50.19.169.37
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
How to trap NaN or Divide by Zero
pcguru333




msg:702062
 2:37 am on Sep 29, 2004 (gmt 0)

I have an XSL doc that is working just fine. I am wanting to add the ability to test values and if they are equal to NaN to set them to zero. In some cases I am dividing numbers to get an average and I want to trap any divide by zero cases and display them as zero. Below is a snippet of my actual code as it is now:


<table>
<tr>
<td style="text-align:center;width:36px;border-left:0px;">No.</td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form101_A_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form101_B_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form102_A_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form102_B1_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form102_B2_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form103_A_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form103_B_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form1_A_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:80px;"><xsl:value-of select="format-number(Form1_B_Number,'#,###,###,##0')"/></td>
<td style="text-align:right;width:88px;"><xsl:value-of select="format-number(Form101_A_Number + Form101_B_Number + Form102_A_Number + Form102_B1_Number + Form102_B2_Number + Form103_A_Number + Form103_B_Number + Form1_A_Number + Form1_B_Number,'#,###,###,##0')"/></td>
</tr>
<tr>
<td style="text-align:center;border-left:0px;">Value</td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form101_A_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form101_B_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_A_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_B1_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_B2_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form103_A_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form103_B_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form1_A_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form1_B_Value,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form101_A_Value + Form101_B_Value + Form102_A_Value + Form102_B1_Value + Form102_B2_Value + Form103_A_Value + Form103_B_Value + Form1_A_Value + Form1_B_Value,'$#,###,###,##0')"/></td>
</tr>
<tr>
<td style="text-align:center;border-left:0px;">Avg.</td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form101_A_Value div Form101_A_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form101_B_Value div Form101_B_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_A_Value div Form102_A_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_B1_Value div Form102_B1_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form102_B2_Value div Form102_B2_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form103_A_Value div Form103_A_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form103_B_Value div Form103_B_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form1_A_Value div Form1_A_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;"><xsl:value-of select="format-number(Form1_B_Value div Form1_B_Number,'$#,###,###,##0')"/></td>
<td style="text-align:right;background-color:#000000;"> </td>
</tr>
</table>

Thanks for any insight

Dan

 

irnbru




msg:702063
 4:02 pm on Oct 4, 2004 (gmt 0)

This hack catches NaN .....

zero.xml
--------

<?xml version="1.0"?>
<root>
<number>2</number>
<number>1</number>
<number>0</number>
<number>-1</number>
<number>-2</number>
</root>

zero.xsl
--------


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:template match="/">
<xsl:for-each select="root/number">

<xsl:variable name="result">
<xsl:call-template name="divide">
<xsl:with-param name="number">
<xsl:value-of select="."/>
</xsl:with-param>
<xsl:with-param name="divisor">
<xsl:value-of select="number(. - 1)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>

<xsl:value-of select="."/>
<xsl:text> / </xsl:text>
<xsl:value-of select="number(. - 1)"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="$result"/>
<xsl:value-of select="$newline"/>

</xsl:for-each>
</xsl:template>

<xsl:template name="divide">

<xsl:param name="number"/>
<xsl:param name="divisor"/>

<xsl:variable name="result">
<xsl:value-of select="$number div $divisor"/>
</xsl:variable>

<xsl:variable name="result2">
<xsl:choose>
<xsl:when test="contains(number($result),'NaN')">
<xsl:text>0</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$result"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- $result gives infinity else $result2 for 0 -->
<xsl:value-of select="$result2"/>
</xsl:template>

</xsl:stylesheet>

which gives


<?xml version="1.0"?>
2 / 1 = 2
1 / 0 = Infinity
0 / -1 = 0
-1 / -2 = 0.5
-2 / -3 = 0.66666666666667

or


<?xml version="1.0"?>
2 / 1 = 2
1 / 0 = 0
0 / -1 = 0
-1 / -2 = 0.5
-2 / -3 = 0.66666666666667

depending whether u use $result or $result2 in the template.

pcguru333




msg:702064
 8:54 pm on Oct 11, 2004 (gmt 0)

Thanks

I was able to use a portion of what you posted to correct my issues. Thanks again for the insight.

Dan

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved