This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: XSLT: SUM function Not working.
- From: "Ragan, Mitch" <mitch dot ragan at boeing dot com>
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Wed, 13 Feb 2002 12:38:04 -0800
- Subject: RE: [xsl] XSLT: SUM function Not working.
- Reply-to: xsl-list at lists dot mulberrytech dot com
I figured it out.
It was not passing the param, so I just moved that block of code and used
Varables.
Thanks~!!
Working Code below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- PRINCIPLE Module -->
<xsl:template match="/">
<HTML>
<BODY>
<H1>Price Analysis</H1>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<!--STOCK Selection-Loop Module -->
<xsl:template match="Stocks" xml:space="preserve">
<xsl:for-each select="Stock/Symbol">
<xsl:sort select="."/>
<h2><xsl:value-of select="."/></h2>
<h3><xsl:text>    Date</xsl:text>
<xsl:text>      Price</xsl:text></h3>
<xsl:call-template name="QuoteData">
<xsl:with-param name="CurrentSymbol"
select= "current()"/>
</xsl:call-template>
<xsl:call-template name="QuoteLowHigh">
<xsl:with-param name="CurrentSymbol"
select= "current()"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<!--QuoteData Module -->
<xsl:template name="QuoteData">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/>
<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]"
mode="Data">
<xsl:sort select="@date"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Quote" mode="Data">
<table>
<tr><td><xsl:value-of select="@date"/></td> <td></td>
<td><xsl:value-of select="."/></td></tr>
</table>
</xsl:template>
<!--QuoteLowHigh Module -->
<xsl:template name="QuoteLowHigh">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/>
<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]"
mode="LowHigh">
<xsl:sort select="."/>
<!--QuoteAvg Computation -->
</xsl:apply-templates>
<xsl:variable name="Cnt">
<xsl:value-of
select="count(//Quote[@symbol=$CurrentSymbol])"/>
</xsl:variable>
<xsl:variable name="Sum">
<xsl:value-of
select="sum(//Quote[@symbol=$CurrentSymbol])"/>
</xsl:variable>
Avg:<xsl:value-of select="( $Sum div $Cnt)"/>
</xsl:template>
<xsl:template match="Quote" mode="LowHigh">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/>
<xsl:value-of select="$CurrentSymbol"/>
<xsl:choose>
<xsl:when test="position()=1">
<xsl:text> Low: </xsl:text>
<xsl:value-of select="."/>
</xsl:when>
<xsl:when test="position()=last()">
<xsl:text> High: </xsl:text>
<xsl:value-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:template>
<!--Blanks Module -->
<xsl:template match ="Quotes"/>
<xsl:template match="Name"/>
<xsl:template match="SharesOutstanding"/>
<xsl:template match="CurrentEarnings"/>
<xsl:template match="Clients"/>
<!-- Debugger Area
-->
</xsl:stylesheet>
-----Original Message-----
From: Ragan, Mitch [mailto:mitch.ragan@boeing.com]
Sent: Wednesday, February 13, 2002 10:06 AM
To: Xsl-List (E-mail)
Subject: [xsl] XSLT: SUM function Not working.
Importance: High
I know this is weak code (I am just learning), but I can not get my XSLT to
compute the Average Price of the /Quotes/Quote, for each Symbol.
See Desired Output (include) Average now = 0. It does not seem to be
seeing the Param telling it which $CurrentSymbol we are now working with in
the loop.
The offending Template is the last one at the bottom of the XSLT. I added
some <!-- comments, at offending spot -->
Any help would be greatly appreciated.
Thanks in advance !!
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- PRINCIPLE Module -->
<xsl:template match="/">
<HTML>
<BODY>
<H1>Price Analysis</H1>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<!--STOCK Selection-Loop Module -->
<xsl:template match="Stocks">
<xsl:for-each select="Stock/Symbol">
<xsl:sort select="."/>
<h2><xsl:value-of select="."/></h2>
<h3><xsl:text>    Date</xsl:text>
<xsl:text>     Price</xsl:text></h3>
<xsl:call-template name="QuoteData">
<xsl:with-param name="CurrentSymbol"
select= "current()"/>
</xsl:call-template>
<xsl:call-template name="QuoteTotals">
<xsl:with-param name="CurrentSymbol"
select= "current()"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<!--QuoteData Module -->
<xsl:template name="QuoteData">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/>
<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]"
mode="Data">
<xsl:sort select="@date"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Quote" mode="Data">
<table>
<tr><td><xsl:value-of select="@date"/></td> <td></td>
<td><xsl:value-of select="."/></td></tr>
</table>
</xsl:template>
<!--Totals Module -->
<xsl:template name="QuoteTotals">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/>
<xsl:apply-templates select="//Quote[@symbol=$CurrentSymbol]"
mode="Totals">
<xsl:sort select="."/>
</xsl:apply-templates>
</xsl:template>
<!-- THIS TEMPLATE WON"T COMPUTE THE SUM" -->
<xsl:template match="Quote" mode="Totals">
<xsl:param name="CurrentSymbol" select =
"Symbol[position()=current()]"/> <!-- param not working -->
<xsl:value-of select="$CurrentSymbol"/> <!-- for
debug, comes up empty-->
<xsl:choose>
<xsl:when test="position()=1">
<xsl:text> Low: </xsl:text>
<xsl:value-of select="."/>
</xsl:when>
<xsl:when test="position()=last()">
<xsl:text> High: </xsl:text>
<xsl:value-of select="."/>
Avg:<xsl:value-of
select="(sum(//Quote[@symbol=$CurrentSymbol]) div last())"/> <!-- Sum
not finding any Quoate data-->
</xsl:when>
</xsl:choose>
</xsl:template>
<!--Blanks Module -->
<xsl:template match ="Quotes"/>
<xsl:template match="Name"/>
<xsl:template match="SharesOutstanding"/>
<xsl:template match="CurrentEarnings"/>
<xsl:template match="Clients"/>
</xsl:stylesheet>
DESIRED OUTPUT (except, with AVG not equal to Zero):
Price Analysis
MCDTA
Date Price
2001-09-15 12.30
2001-10-15 14.95
2001-11-15 16.00
2001-12-15 24.5
2002-02-15 28.0
Low: 12.30 High: 28.0 Avg:0
MSFT
Date Price
2001-09-01 53.50
2002-02-15 65.25
Low: 53.50 High: 65.25 Avg:0
TK
Date Price
2001-08-07 30
2001-09-30 28.40
2001-10-24 26.95
2001-11-11 31.125
2002-02-15 38.875
Low: 26.95 High: 38.875 Avg:0
XML:
<?xml version="1.0"?>
<StockData
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Mission4.xsd">
<Version date="2002-02-15" time="09:01:45"/>
<Stocks>
<Stock>
<Name>TeeKay Shipping</Name>
<Symbol exchange="NYSE">TK</Symbol>
<SharesOutstanding>3000000</SharesOutstanding>
<CurrentEarnings>128000000</CurrentEarnings>
</Stock>
<Stock>
<Name>McData Corporation</Name>
<Symbol>MCDTA</Symbol>
<SharesOutstanding>3000000</SharesOutstanding>
<CurrentEarnings>128000000</CurrentEarnings>
</Stock>
<Stock>
<Name>Microsoft Corporation</Name>
<Symbol>MSFT</Symbol>
<SharesOutstanding>125000000</SharesOutstanding>
<CurrentEarnings>26000000000</CurrentEarnings>
</Stock>
</Stocks>
<Quotes>
<Quote symbol="TK" date="2001-09-30">28.40</Quote>
<Quote symbol="TK" date="2001-10-24">26.95</Quote>
<Quote symbol="TK" date="2001-08-07">30</Quote>
<Quote symbol="TK" date="2001-11-11">31.125</Quote>
<Quote symbol="TK" date="2002-02-15">38.875</Quote>
<Quote symbol="MCDTA" date="2001-09-15">12.30</Quote>
<Quote symbol="MCDTA" date="2001-10-15">14.95</Quote>
<Quote symbol="MCDTA" date="2001-11-15">16.00</Quote>
<Quote symbol="MCDTA" date="2001-12-15">24.5</Quote>
<Quote symbol="MCDTA" date="2002-02-15">28.0</Quote>
<Quote symbol="MSFT" date="2001-09-01">53.50</Quote>
<Quote symbol="MSFT" date="2002-02-15">65.25</Quote>
</Quotes>
<Clients>
<Client>
<Name>Philip Su</Name>
<Portfolio>
<Stock>
<Symbol>TK-C</Symbol>
<Purchase date="2001-10-24">
<Brokerage>Datek</Brokerage>
<Shares>200</Shares>
<Commission>9.99</Commission>
</Purchase>
</Stock>
<Stock>
<Symbol>MCDTA-C</Symbol>
<Purchase date="2001-11-12">
<Shares>400</Shares>
<Commission>12.00</Commission>
</Purchase>
</Stock>
<Stock>
<Symbol>MSFT-C</Symbol>
<Purchase date="2001-09-01">
<Brokerage>TD Waterhouse</Brokerage>
<Total>32635.00</Total>
<Commission>10.00</Commission>
</Purchase>
</Stock>
</Portfolio>
</Client>
</Clients>
</StockData>
Mitch K. Ragan
Boeing Commercial Aircraft Group
Global Electronic Commerce
Senior Systems Analyst
Primary: 425-266-3155
Secondary: 425-237-1735
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list