Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do mathematical operations using xslt

Tags:

xml

xslt

Below is sample xml file:

<?xml version="1.0" ?>
<emp>
    <emp_name>jhon</emp_name>
    <emp_sal>2000</emp_sal>
    <emp_sal1>1000</emp_sal1>
    <emp_total>10</emp_total>
</emp>

Based on above xml data:, i want to calculate : emp_sal * emp_sal1 / emp_total in XSLT.

I am trying to with xsl:template , as i am newbie to xslt .can anyone please help me.

Regards,

Chaitu


1 Answers

It's almost that. In XPath you have to use div instead of /:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:template match="emp">
        <result>
            <xsl:value-of select="emp_sal * emp_sal1 div emp_total"/>
        </result>
    </xsl:template>
</xsl:stylesheet>

This produces:

<result>200000</result>

The template maches a node in the source document (<emp>), which creates the context for the node names used in the XPath expression (emp_sal, emp_sal1 and emp_total) in the xs:value-of select.

like image 161
helderdarocha Avatar answered Dec 08 '25 11:12

helderdarocha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!