[xquery-talk] avoid divide by zero

Liam Quin liam at w3.org
Fri May 26 07:06:35 PDT 2006


On Wed, May 24, 2006 at 11:59:21PM -0700, fatma helmy wrote:
> regarding my xquery
> <RATIO> 
> {string( round( count($paths[.=$p]) div
> count($paths[.=$papa]) * 100 ) )} 
> </RATIO> 
> 
> I have a statement that might cause divide by zero, is
> there any statement to replace zero by one when it
> occurs. 

You have to ask what the RATIO element is representing in
this case; perhaps it would be better to omit the element:

{
  if (count($path[.=$papa]))
  then
    <RATIO>
      {
        round(
          count($paths[. = $p]) div
          count($paths[. = $papa]) * 100
        )
      } 
    </RATIO>
  else ()
}

Generally, if you find yourself using a different value in
a calculation to avoid an error, it's worth asking if you
should be doing the calculation at all.

Liam

-- 
Liam Quin, W3C XML Activity Lead, http://www.w3.org/People/Quin/
http://www.holoweb.net/~liam/ * http://www.fromoldbooks.org/


More information about the talk mailing list