I have XML content like this,
<doc>
<p>20</p>
<p>20</p>
<p>20</p>
<p>100</p>
<t>160</t>
</doc>
There are four <p>
elements containing numbers, and <t>
represents the total of above four <p>
elements.
My objective is to calculate the percentage of these <p>
elements and display the result.
This is XSL code I've written for it,
<xsl:variable name="val1" select="abc:get-int-value(doc/p[1]/text())" as="xs:double"/>
<xsl:variable name="val2" select="abc:get-int-value(doc/p[2]/text())" as="xs:double"/>
<xsl:variable name="val3" select="abc:get-int-value(doc/p[3]/text())" as="xs:double"/>
<xsl:variable name="val4" select="abc:get-int-value(doc/p[4]/text())" as="xs:double"/>
<xsl:variable name="valTotal" select="abc:get-int-value(doc/t/text())" as="xs:double"/>
<xsl:variable name="precentage1" select="round(($val1 div $valTotal)*100)" as="xs:double"/>
<xsl:variable name="precentage2" select="round(($val2 div $valTotal)*100)" as="xs:double"/>
<xsl:variable name="precentage3" select="round(($val3 div $valTotal)*100)" as="xs:double"/>
<xsl:variable name="precentage4" select="round(($val4 div $valTotal)*100)" as="xs:double"/>
<xsl:template match="doc">
<xsl:value-of select="$precentage1"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$precentage2"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$precentage3"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$precentage4"/>
</xsl:template>
<xsl:function name="abc:get-int-value" as="xs:double">
<xsl:param name="text" as="xs:string"/>
<xsl:analyze-string select='$text' regex='\d+'>
<xsl:matching-substring>
<xsl:sequence select="number(.)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
But, it gives percentage values as, 13, 13, 13 and 63. So the total of those numbers is 102 (literally it should be 100, 99, or 101). It seems the percentage value of p[4] has been rounded up to 63 instead of 62.
Why is this happening and how can I fix this?