473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[XSLT] averaging

I have the following XML file (simplified from the actual):

<r>
<o><n>1</n><si>s</si><v1>1</v1><v2>2</v2><v3>3</v3></o>
<o><n>2</n><si>i</si><v1>4</v1><v2>5</v2><v3>6</v3></o>
<o><n>3</n><si>s</si><v1>7</v1><v2>8</v2><v3>9</v3></o>
<o><n>5</n><si>i</si><v1>10</v1><v2>11</v2><v3>12</v3></o>
<o><n>6</n><si>i</si><v1>13</v1><v2>14</v2><v3>15</v3></o>
</r>

and the following stylesheet (again simplified):
<xsl:template match="/"><html><body>< table>
<xsl:for-each select="r/o"><tr>
<td><xsl:valu e-of select="n" /></td>
<td><xsl:valu e-of select="si" /></td>
<td><xsl:valu e-of select="v1" /></td>
<td><xsl:valu e-of select="v2" /></td>
<td><xsl:valu e-of select="v3" /></td>
<td><xsl:valu e-of
select="(v1 + (2 * v2) + (3 * v3)) div (v1 + v2 + v3)" /></td>
</tr></xsl:for-each>
<tfoot>
<tr><th scope="row" colspan="5">Ave rage when "s"</th>
<td> [...] </td></tr>
<tr><th scope="row" colspan="5">Ave rage when "i"</th>
<td> [...] </td></tr></tfoot></table></body></html>

In place of the '[...]' I wish to have the weighted averages, as follows.
In place of the first '[...]' I want
( 1 + 2*2 + 3*3 + 7 + 2*8 + 3*9 ) / ( 1 + 2 + 3 + 7 + 8 + 9 )
(which is the same as the weighted average of the various weighted
averages already computed), and in place of the second '[...]' I want
( 4 + 2*5 + 3*6 + 10 + 2*11 + 3*12 + 13 + 2*14 + 3* 15 ) divided
by ( 4+ 5+ 6+ 10 + 11 + 12 + 13 + 14 + 15 ).

Is there a way to do this in XSLT?

Thanks,

Michael Hamm It's not who you know, it's whom.
AM, Math, Wash. U. St. Louis Joan Rivers
ms****@math.wus tl.edu Fine print:
http://www.math.wustl.edu/~msh210/ ... legal.html
Dec 28 '05 #1
2 1846
Michael Hamm wrote:

In place of the '[...]' I wish to have the weighted averages, as follows.
In place of the first '[...]' I want
( 1 + 2*2 + 3*3 + 7 + 2*8 + 3*9 ) / ( 1 + 2 + 3 + 7 + 8 + 9 )
(which is the same as the weighted average of the various weighted
averages already computed), and in place of the second '[...]' I want
( 4 + 2*5 + 3*6 + 10 + 2*11 + 3*12 + 13 + 2*14 + 3* 15 ) divided
by ( 4+ 5+ 6+ 10 + 11 + 12 + 13 + 14 + 15 ).

Is there a way to do this in XSLT?
Sure. One way to do it directly is this:

<xsl:value-of
select="(sum(o[si='s']/v1)+2*sum(o[si='s']/v2)+3*sum(o[si='s']/v3)) div
(sum(o[si='s']/v1)+sum(o[si='s']/v2)+sum(o[si='s']/v3))"/>

That works, but it has some limitations. First, it's very long and hard
to read. Second, you have to redo the entire calculation for [si='i']
which is a maintenance problem. Third, if you ever need to change the
calculation, you'll have to change it in multiple places, which is
another maintenance problem. A better strategy is to separate the
selection from the calculation, and the way to do that is to use templates.

Also, I notice that you use xsl:for-each in your example where
xsl:apply-templates would be a better choice. Early on when I was
learning XSLT, it became clear to me that whenever I found myself using
xsl:for-each, it was probably better done using xsl:apply-templates.

I wrote a new XSLT which is longer, but it has the advantage that it
doesn't have the limitations mentioned above.

Note that the calculations use recursion, which is the usual way to get
things done in XSLT. You'll find it useful as you create more complex
calculations in XSLT.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"

<xsl:output method="html" indent="yes" />
<xsl:template match="r">
<html>
<body>
<table>
<thead>
<tr>
<th>n</th>
<th>si</th>
<th>v1</th>
<th>v2</th>
<th>v3</th>
<th>calculation </th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="o" />
</tbody>
<tfoot>
<tr>
<th scope="row" colspan="5">Ave rage when "s"</th>
<td><xsl:call-template name="avg">
<xsl:with-param name="list" select="o[si='s']"/>
</xsl:call-template></td>
</tr>
<tr>
<th scope="row" colspan="5">Ave rage when "i"</th>
<td><xsl:call-template name="avg">
<xsl:with-param name="list" select="o[si='i']"/>
</xsl:call-template></td>
</tr>
</tfoot>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="o">
<tr>
<td><xsl:valu e-of select="n" /></td>
<td><xsl:valu e-of select="si" /></td>
<td><xsl:valu e-of select="v1" /></td>
<td><xsl:valu e-of select="v2" /></td>
<td><xsl:valu e-of select="v3" /></td>
<td><xsl:call-template name="avg">
<xsl:with-param name="list" select="."/>
</xsl:call-template></td>
</tr>
</xsl:template>

<xsl:template match="o" mode="calcnum">
<xsl:value-of select="(v1 + (2 * v2) + (3 * v3))"/>
</xsl:template>

<xsl:template match="o" mode="calcdenom ">
<xsl:value-of select="(v1 + v2 + v3)" />
</xsl:template>

<xsl:template name="avg">
<xsl:param name="list"/>
<xsl:variable name="num">
<xsl:call-template name="numerator ">
<xsl:with-param name="list" select="$list"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="denom">
<xsl:call-template name="denominat or">
<xsl:with-param name="list" select="$list"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$num div $denom"/>
</xsl:template>

<xsl:template name="numerator ">
<xsl:param name="list"/>
<xsl:choose>
<xsl:when test="$list">
<xsl:variable name="first">
<xsl:apply-templates select="$list[1]" mode="calcnum"/>
</xsl:variable>
<xsl:variable name="rest">
<xsl:call-template name="numerator ">
<xsl:with-param name="list" select="$list[position()!=1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$first + $rest"/>
</xsl:when>
<xsl:otherwise> 0</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="denominat or">
<xsl:param name="list"/>
<xsl:choose>
<xsl:when test="$list">
<xsl:variable name="first">
<xsl:apply-templates select="$list[1]" mode="calcdenom "/>
</xsl:variable>
<xsl:variable name="rest">
<xsl:call-template name="denominat or">
<xsl:with-param name="list" select="$list[position()!=1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$first + $rest"/>
</xsl:when>
<xsl:otherwise> 0</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
For completeness, here's your original data file:

<?xml version="1.0"?>
<r>
<o><n>1</n><si>s</si><v1>1</v1><v2>2</v2><v3>3</v3></o>
<o><n>2</n><si>i</si><v1>4</v1><v2>5</v2><v3>6</v3></o>
<o><n>3</n><si>s</si><v1>7</v1><v2>8</v2><v3>9</v3></o>
<o><n>5</n><si>i</si><v1>10</v1><v2>11</v2><v3>12</v3></o>
<o><n>6</n><si>i</si><v1>13</v1><v2>14</v2><v3>15</v3></o>
</r>

Ed
Dec 30 '05 #2
On Fri, 30 Dec 2005, Ed Beroset wrote, in small part:
Sure.


Wow. Thanks so much for your response -- beyond the call of, Usenet duty.
Or whatever. Anyway, thanks a lot.

Happy new year,

Michael Hamm It's not who you know, it's whom.
AM, Math, Wash. U. St. Louis Joan Rivers
ms****@math.wus tl.edu Fine print:
http://www.math.wustl.edu/~msh210/ ... legal.html
Jan 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3891
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a "crumb-trail" of links. I'm transforming the XML with XSLT through Saxon. The nav menu and "crumb-trail" show the user where they are within the site...
1
3587
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by version 2 of XSLT processor based on some condition. Q) How and where shall I write logic or import desirable XSLT on the Fly ? Q) When we call...
3
2185
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about the development environment or recommend me some other that you know. XML IDE's - http://xslt-process.sourceforge.net -...
1
2194
by: Rotten Spice | last post by:
Hello all, I am still pretty new to Access but I do have a grasp on some basic functions. I am having a problem with averaging and I think I'm trying to do something a little too complex and maybe I even have it set up wrong. I am putting some evaluation forms online. Each form/table is different, apart from the first 10 questions...
1
2404
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT stylesheets, that generates .NET assemblies) 2. Performance improvements in the XslCompiledTransform
12
11568
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I don't want to have to use some external program or script to parse the csv first if possible
1
1979
by: JJ R | last post by:
I am trying to develop a simple and efficient to calculate averages. For example I want to calculate the averages of the Values for rows 1 and 2 and use ID 1 as the row with the average result. Table Exmple ID Value1 Value2 Value3 1 2 3 1 2 5 4 32 3 4 45 6 4 12 78 12
2
22756
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most...
1
1692
by: Kid Programmer | last post by:
Hello guys. Whenever I am learning a new programming language I like to learn the basics (variables, loops, conditionals etc) by writing a program that will calculate averages. I wrote a simple number averaging program. But when I run it in Eclipse or the command line and I enter: 1 3 5.5 It says the average is 3.1666666666666665 when...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.