Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 28th, 2011, 03:56 AM
Registered User
 
Join Date: Sep 2011
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
Unhappy Problem with variables...

Hi there gurus!

I am new to XML and XSLT, so my quiestion should be an easy one for you.
What i am trying to do is get average from CD 'price' element.

I failed using 'sequence' to get all avalues from price element. So i decided to 1) get SUM on price and store it in 'mySum' variable 2) get COUNT of the CDs and store it in 'myCount' 3) get value of '$mySum/$myCount' and store it in myRes variable.

The following code works fine and i see the values:
Code:
<xsl:variable name="mySum" select="sum(catalog/cd/price)"></xsl:variable>
<xsl:variable name="myCount" select="count(catalog/cd)"></xsl:variable>
<td><xsl:value-of select="$mySum"/></td>
<td><xsl:value-of select="$myCount"/></td>
but when i try:
Code:
<xsl:variable name="mySum" select="sum(catalog/cd/price)"></xsl:variable>
<xsl:variable name="myCount" select="count(catalog/cd)"></xsl:variable>
<td><xsl:value-of select="$mySum/$myCount"/></td>
or
Code:
<xsl:variable name="mySum" select="sum(catalog/cd/price)"></xsl:variable>
<xsl:variable name="myCount" select="count(catalog/cd)"></xsl:variable>
<xsl:variable name="myRes" select="$mySum/$myCount"/></xsl:variable>
<td><xsl:value-of select="$myRes"/></td>
it fails...

Can you guys help me out here?

Thanks in advance!
 
Old September 28th, 2011, 05:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well with the XSLT/XPath syntax the "/" character is used to separate steps in a path expression, not as a numerical operator. If you look at http://www.w3.org/TR/xpath/#numbers then you see the "div" is the operator for floating point division.
Code:
select="$mySum div $myCount"
You should also be aware that with XSLT/XPath 2.0 there is a function avg http://www.w3.org/TR/xpath-functions/#func-avg to compute the average.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
Transformer (September 28th, 2011)
 
Old September 28th, 2011, 06:22 AM
Registered User
 
Join Date: Sep 2011
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi Martin!

Thanks for the reply, you are absolutely right about that. I have just realized that myself and i opened my topic to write that i have found that you are already pointed to it.

I don't know why i didn't saw this at first time. Anyway, i'll be more accurate from now on.

About that #func-avg... I have found it lately, but had some problems with sequence, so that is why i decided to calculate by hand.

One more question: current select="$mySum div $myCount" gives me plenty numbers after coma. How can i set precision of that variable to show only 2 numbers after coma? I tried to put 'as="xs:decimal*"' in <xsl:variable> but the error is: "'as' is an invalid attribute for the 'xsl:variable' element."
Any ideas?

Thanks again!
XSLTransformer.
 
Old September 28th, 2011, 06:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

XSLT has format-number function http://www.w3.org/TR/xslt#format-number:
Code:
format-number($mySum div $myCount, '#.00')
As for your failing attempts to use the avg function or the as attribute, make sure you use an XSLT 2.0 processor like Saxon 9, AltovaXML, XQSharp. Then you can use those features, otherwise not.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
Transformer (September 28th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem when subtracting variables Neal Excel VBA 2 January 6th, 2009 01:06 PM
Problem With variables and forms marcrock Classic ASP Databases 1 November 8th, 2005 02:02 PM
Problem with defining variables Asix Beginning PHP 6 February 14th, 2005 01:32 AM
Problem in Adding Variables with decimals sa_moizatyahoo Classic ASP Basics 1 April 3rd, 2004 06:17 AM
Session variables problem rajanikrishna Beginning PHP 1 December 4th, 2003 11:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.