I've got an XML:

<?xml version="1.0" standalone="yes"?>

and a XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:template match = "/" >


<br />
<table border="0">

<td class="headerClass">name</td>
<td class="headerClass">salary1</td>
<td class="headerClass">salary2</td>

<xsl:for-each select="//root/profile">

<td><center><xsl:value-of select="name"/></center></td>
<td><center><xsl:value-of select="salary1"/></center></td>
<td><center><xsl:value-of select="salary2"/></center></td>



<td><center><xsl:value-of select="sum(/root/profile/salary1)"/></center></td>
<td><center><xsl:value-of select="sum(/root/profile/salary2)"/></center></td>


<xsl:variable name="cal">
<xsl:for-each select="/root/profile">
<xsl:value-of select="(salary1 * 100) div (salary1 + salary2)"/>


<td><center><xsl:value-of select="$cal"/></center></td>




and now I've got a variable "cal" which consist of two numbers and looks like 66.66666666666667157.142857142857146, but I wanna to separate these numbers and add it together, then divide by the number of the numbers, in my example:


divide into:

66.666666666666671 and 57.142857142857146

then (66.666666666666671 + 57.142857142857146)/2

I've got XSLT version 1.0, and using VS 2008. Could anyone help me, please?

Here is a stylesheet which will do what you want to do. I have simplied it to just output the result as a text value. It uses recursion. You cannot use a for loop to do what you want to do.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />

<xsl:template match="root">
  <xsl:apply-templates select="profile[1]" mode="demo" />

<xsl:template match="profile" mode="demo">
  <xsl:param name="col" select="0" />
  <xsl:param name="cnt" select="0" />

    <xsl:when test="following-sibling::profile">
      <xsl:apply-templates select="following-sibling::profile[1]" mode="demo">
         <xsl:with-param name="col" select="$col + ((salary1 * 100) div (salary1 + salary2))" />
         <xsl:with-param name="cnt" select="$cnt + 1" />
      <xsl:value-of select="( ($col + ((salary1 * 100) div (salary1 + salary2))) div ($cnt + 1))" />


The reason you have to use recursion is that XSLT is a declarative language. Once a variable has been declared to be some value, it is immutable. However the twist is that a variable is immutable only in the instance of the template it was instantiated in and this is where recusion comes into play.

This article has been dead for over six months. Start a new discussion instead.