XSLT and the math functions

It’s interesting that a lot of math functions in XSLT 1.0 (multiplication, rounding, div, …) depend on the XSLT processor/platform and might produce different outputs. So, you might need to leave all of these operations to the processes outside of XSLT, or at least test them carefully.

Just an example:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
            =<xsl:value-of select="format-number(round( 0.005 * 100) div 100, '0.00')" />
            =<xsl:value-of select="format-number(( 0.005 * 100) div 100, '0.00')" />
            =<xsl:value-of select="format-number(round( 9999.005 * 100) div 100, '0.00')" />
            =<xsl:value-of select="format-number(( 9999.005 * 100) div 100, '0.00')" />
      </xsl:template>
</xsl:stylesheet>

Outputs:

MS Visual Studio on Win 7:

Altova XML Spy:

xslttest.appspot.com

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

So finally I had to create my own rounding template, “quick and dirty” but it works.

<xsl:template name="custom_round">
   <xsl:param name="n" />

   <xsl:variable name="n2">
      <!-- convert all to positive -->
      <!--xsl:choose>
         <xsl:when test="contains($n, '-')">
            <xsl:value-of select="substring-after($n, '-')"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$n"/>
         </xsl:otherwise>
      </xsl:choose-->

      <!-- use both positive and negative -->
      <xsl:value-of select="$n"/>
   </xsl:variable>

   <xsl:choose>
      <xsl:when test="not(contains($n2, '.'))">
         <!-- X.0 -->
         <xsl:call-template name="addLeadingZeros">
            <xsl:with-param name="str1" select="concat($n2, '.00')" />
            <xsl:with-param name="len" select="10" />
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:choose>
            <xsl:when test="string-length( substring-after($n2, '.') ) = 1">
               <!-- X.X -->
               <xsl:call-template name="addLeadingZeros">
                  <xsl:with-param name="str1" select="concat($n2, '0')" />
                  <xsl:with-param name="len" select="10" />
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="string-length( substring-after($n2, '.') ) = 2">
               <!-- X.XX -->
               <xsl:call-template name="addLeadingZeros">
                  <xsl:with-param name="str1" select="$n2" />
                  <xsl:with-param name="len" select="10" />
               </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
               <!-- X.XXX... -->
               <xsl:variable name="tmp_num" select="substring($n2, 1, string-length( substring-before($n2, '.') ) + 3)" />

               <xsl:choose>
                  <xsl:when test="number( substring($n2, string-length( substring-before($n2, '.') ) + 4, 1)) &gt;= 5">
                     <!-- 3rd number after the . is greater than 5 or equal -->

                     <xsl:variable name="n3">
                        <xsl:choose>
                           <xsl:when test="contains($n, '-')">
                              <xsl:value-of select="number($tmp_num) - 0.01" />
                           </xsl:when>
                           <xsl:otherwise>
                              <xsl:value-of select="number($tmp_num) + 0.01" />
                           </xsl:otherwise>
                        </xsl:choose>
                     </xsl:variable>

                     <!--
                        we need to call the same template because 1.23 + 0.01 might be equal to 1.24000000000000001
                     -->

                     <xsl:call-template name="custom_round">
                        <xsl:with-param name="n" select="$n3" />
                     </xsl:call-template>
                  </xsl:when>
                  <xsl:otherwise>
                     <!-- 3rd number after the . is less than 5-->
                     <xsl:call-template name="addLeadingZeros">
                        <xsl:with-param name="str1">
                           <xsl:value-of select="$tmp_num"/>
                        </xsl:with-param>
                        <xsl:with-param name="len" select="10" />
                     </xsl:call-template>
                  </xsl:otherwise>
               </xsl:choose>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template name="addLeadingZeros">
   <xsl:param name="str1" />
   <xsl:param name="len" />

   <xsl:variable name="str2">
      <xsl:choose>
         <xsl:when test="contains($str1, '-')">
            <xsl:value-of select="substring-after($str1, '-')"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$str1"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>

   <xsl:variable name="str3" select="concat('00000000000000000', $str2)" />

   <xsl:if test="contains($str1, '-')">
      <xsl:value-of select="'-'"/>
   </xsl:if>
   <xsl:value-of select="substring($str3, string-length($str3) - number($len) + 1)"/>
</xsl:template>

Gennady Kim

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s