0

I've got an XML:

<?xml version="1.0" standalone="yes"?>
<root>
<profile>
<name>xyz</name>
<salary1>4</salary1>
<salary2>2</salary2>
</profile>
<profile>
<name>mno</name>
<salary1>8</salary1>
<salary2>6</salary2>
</profile>
</root>

and a XSLT:

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

<xsl:template match = "/" >

<html>
<head><title></title></head>

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

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

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

<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>

</tr>
</xsl:for-each>

<tr>

<td>Sum</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>

</tr>

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

<tr>

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

</tr>

</table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

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:

66.66666666666667157.142857142857146

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?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by fpmurphy
0

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>

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

  <xsl:choose>
    <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:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="( ($col + ((salary1 * 100) div (salary1 + salary2))) div ($cnt + 1))" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.