user3457336
user3457336

Reputation:

how to do mathematical operations using 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

Upvotes: 3

Views: 4808

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

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.

Upvotes: 2

Related Questions