开发者

What is the ROUND function behavior in Orbeon Forms?

In Orbeon Forms (dev-post-3.7.1.200911140400) we have code in an XPL that do some calculation, and part of this calculation is we ROUND the result to 2 decimal places. Below is an example of the code we use to do the calculation:

<xsl:when test="$total_c_w != 0">
    <gpa><xsl:value-of select="(round(($total_p_c_w div $total_c_w) * 100) div 100)"/></gpa>
</xsl:when>

According to the standard XPATH documentation on the ROUND function; Rounds a numeric value to the nearest whole number, rounding x.5 towards positive infinity.

But we encounter a case where the ROUND function is rounding 237.5 to 237, instead of 238. This is just an example, there are other cases where a similar problem involving x.5 is happening.

For example mention, the values in the calculation are:

$total_p_c_w = 7.6, $total_c_w = 3.2

=====================================================

Alex, Thanks for the guidance. I did some more debugging and found something weird please refer to the following XSL code that I tested with on the latest Orbeon Forms 3.9.0.201105152046 CE.

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
    <main>
        <xsl:variable name="tppcw" select="/root/studentpcw/total_p_c_w" as="xs:double"/>
        <xsl:variable name="tccw" select="/root/studentpcw/total_c_w" as="xs:double"/>              
        <xsl:variable name="tpcw" select="sum(/root/studentpcw/total_p_c_w)"/>
        <xsl:variable name="tcw" select="sum(/root/studentpcw/total_c_w)"/>         
        <xsl:variable name="total_p_c_w" select="7.6"/>
        <xsl:variable name="total_c_w" select="3.2"/>       
    <var1><xsl:value-of select="sum(/root/studentpcw/total_p_c_w)"/></var1>
    <var2><xsl:value-of select="sum(/root/studentpcw/total_c_w)"/></var2>
    <var3><xsl:value-of select="$total_p_c_w"/></var3>
    <var4><xsl:value-of select="$total_c_w"/></var4>        
    <result1>

        <xsl:value-of select="round(($total_p_c_w div $total_c_w) * 100)"/>
    </result1>
    <result2>

        <xsl:value-of select="round(($tppcw div $tccw) * 100)"/>
    </result2>
    </main>
    </xsl:template>
</xsl:transform>

Apply the above code at this sample document:

<root>
  <studentpcw>
  <total_p_c_w>7.6</total_p_c_w>
  <total_c_w>3.2</total_c_w>
  </studentpcw>
</root>

The result is quite unexpected;

<main xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <var1>7.6</var1>
   <var2>3.2</var2>
   <var3>7.6</var3>
   <var4>3.2</var4>
   <result1>238</result1>
   <result2>237</result2>
</main>

The problem is that if I assign a literal number to the variable and use that variable in the rounding fun开发者_如何学Goction, the result is as I expected. If I select the value from a node and assign to the variable and use that variable in the rounding function, the result is wrong or unexpected.


I get a result of 238 running the following stylesheet in a nightly build through the XSLT sandbox (which, if you have Orbeon Forms installed locally, you can access through http://localhost:8080/orbeon/sandbox-transformations/xslt/).

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
        <result>
            <xsl:variable name="total_p_c_w" select="7.6"/>
            <xsl:variable name="total_c_w" select="3.2"/>
            <xsl:value-of select="round(($total_p_c_w div $total_c_w) * 100)"/>
        </result>
    </xsl:template>
</xsl:transform>

I believe this is the result you expected, but that you might be getting something different with the version you are using. Could you try the above example in the XSLT sandbox of the version you are using? If you're getting 237 instead of 238, then this is a sign that this issue has been fixed, and I would then recommend you to upgrade to a newer version of Orbeon Forms (currently 3.9).

(Note that this is most likely not something in Orbeon Forms per se, but in the XSLT implementation Orbeon Forms uses, which is the excellent Saxon.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜