[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT: SUM function Not working.

Subject: RE: XSLT: SUM function Not working.
From: "Ragan, Mitch" <mitch.ragan@xxxxxxxxxx>
Date: Wed, 13 Feb 2002 12:38:04 -0800
xslt sum count
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>&#160;&#160;&#160;&#160;Date</xsl:text>
	
<xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;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@xxxxxxxxxx]
Sent: Wednesday, February 13, 2002 10:06 AM
To: Xsl-List (E-mail)
Subject:  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>&#160;&#160;&#160;&#160;Date</xsl:text>
	
<xsl:text>&#160;&#160;&#160;&#160;&#160;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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.