Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] Calculating a Moving Average

From: "Jack Ratcliff" <jratcliff@------------>
To:
Date: 6/1/2004 8:00:00 AM
> I am trying to calculate a moving average based on the below XML, 
> according to a number of days parameter. Ie 20 day moving average, 
> would sum the last (starting at the end 20040528) 20 days 
> closing price <cl> and divide by 20.


John, I think this would work for you without having to use any
extensions.  The key was to get the 'dateFiltered' variable to only
contain the nodes where their position is greater than the (last node
minus the varDays parameter).


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

<xsl:param name="varDays" select="20"/>

<xsl:output method="html" indent="yes" version="4.0" />


<xsl:variable name="dataFiltered" select="asx/day[position() &gt;
(last() - $varDays)]" />

<xsl:template match="/">
 <html>
  <head><title>Rolling Average</title></head>
  <body>
   Average = <xsl:value-of select="sum($dataFiltered/cl) div
count($dataFiltered)"/>
   <!-- here are the details that made up the average -->
   <table border="1">
    <xsl:apply-templates select="$dataFiltered"/>
   </table>
  </body>
 </html>
</xsl:template>


<xsl:template match="day">
 <tr>
  <td><xsl:value-of select="@id"/></td>
  <td><xsl:value-of select="cl"/></td>
 </tr>
</xsl:template>

</xsl:stylesheet>



Cheers,
Jack



**********************************************************************************
This e-mail and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to which they are addressed.  It is not an offer or acceptance, and it is not intended to be all or part of an agreement.  This communication may contain material protected by the attorney-client privilege.  If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, be advised that you have received this e-mail in error and that any review, use, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited.  If you have received this e-mail in error, please notify the sender immediately by return e-mail and delete this e-mail from your system.  The sender does not accept liability for any damage caused by any virus transmitted by this e-mail or any errors or omissions in the contents of this message which arise as a result of e-mail transmission.

**********************************************************************************


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent