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 March 11th, 2010, 11:41 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Problem with sum function

I've got xml:

<root>
<profile>
<name>xyz<name>
<salary1>4<salary1>
<salary2>2<salary2>
</profile>
<profile>
<name>mno<name
<salary1>8<salary1>
<salary2>6<salary2>
</profile>
</root>

I wrote such a code of xsl:

<xsl:value-of select="sum((/root/profile/salary1/text()*100) div (/root/profile/salary1/text() + (/root/profile/salary2/text()*100)))"/>

but the sum function doesn't work in this case, what should I correct to calculate value:

(salary1*100)/(salary1+salary2) of all profiles? In this example:

4*100/(4+2) + 8*100/(8+6)

Thanks for any advice.
 
Old March 11th, 2010, 12:24 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you use XSLT 2.0 or 1.0?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 11th, 2010, 01:17 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 2.0 you can use
Code:
    <xsl:value-of select="sum(
                            for $p in /root/profile
                            return ($p/salary1 * 100) div ($p/salary1 + $p/salary2)
                          )"/>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2010, 05:10 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Martin thanks, but unfortunatelly I've got XSLT version 1.0. Is it possible to solve my problem in this case? Do I have to migrate to version 2.0?
 
Old March 15th, 2010, 07:00 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:stylesheet  version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
  
<xsl:output encoding="utf-8" indent="yes" method="html"/>
 
<xsl:template match="/">
<xsl:variable name="sum">
<xsl:for-each select="//profile">
<pro><xsl:value-of select="(salary1 * 100) div (salary1 + salary2)"/></pro>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exsl:node-set($sum)/pro)"/>
</xsl:template>
__________________
Rummy
 
Old March 15th, 2010, 09:22 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I've tried this , but got message:


"Cannot find the script or external object that implements prefix 'exsl'."

But if I tried this one:

<xsl:variable name="sum">
<xsl:for-each select="//profile">
<pro><xsl:value-of select="salary1"/></pro>
</xsl:for-each>
<xsl:value-of select="sum(pro)"/>
</xsl:variable>

the value of variable "sum" is: salary1salary2 (in my case 48), variable pro has a table of salary1 values.
 
Old March 15th, 2010, 09:26 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT 1.0 processor do you use? Does it have an extension function to convert a result tree fragment into a node-set?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2010, 09:55 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hmm.. I don't know processor type. I'm using Visual Studio 2008, if it will help.
 
Old March 15th, 2010, 09:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Visual Studio uses System.Xml.Xsl.XslCompiledTransform which supports the exsl:node-set function. Did you put the
Code:
xmlns:exsl="http://exslt.org/common"
into your stylesheet?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 15th, 2010, 10:40 AM
Registered User
 
Join Date: Mar 2010
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

OK, to sum up:

I've got an XML:

<?xml version="1.0" standalone="yes"?>
<root>
<profile>
<name>xyz</name>
<salary1>4</salary1>
<salary2>2</salary2>
</profile>
<profile>
<name>mno</name>
<salary1>8</salary1>
<salary2>6</salary2>
</profile>
</root>

and a XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0">

<xsl:template match = "/" >

<html>
<head><title></title></head>

<body>
<br />
<table border="0">
<tr>

<td class="headerClass">name</td>
<td class="headerClass">salary1</td>
<td class="headerClass">salary2</td>

</tr>
<xsl:for-each select="//root/profile">
<tr>

<td><center><xsl:value-of select="name"/></center></td>
<td><center><xsl:value-of select="salary1"/></center></td>
<td><center><xsl:value-of select="salary2"/></center></td>

</tr>
</xsl:for-each>

<tr>

<td>Sum</td>
<td><center><xsl:value-of select="sum(/root/profile/salary1)"/></center></td>
<td><center><xsl:value-of select="sum(/root/profile/salary2)"/></center></td>

</tr>

<xsl:variable name="cal">
<xsl:for-each select="/root/profile">
<xsl:value-of select="(salary1 * 100) div (salary1 + salary2)"/>
</xsl:for-each>
</xsl:variable>

<tr>

<td>Calculations</td>
<td><center><xsl:value-of select="$cal"/></center></td>

</tr>

</table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>


and now I've got a variable "cal" which consist of two numbers and looks like 66.66666666666667157.142857142857146, but I wanna to separate these numbers and add it together, then divide by the number of the numbers, in my example:

66.66666666666667157.142857142857146

divide into:

66.666666666666671 and 57.142857142857146

then (66.666666666666671 + 57.142857142857146)/2

I've got XSLT version 1.0, and using VS 2008. Could anyone help me, please?

Last edited by patrickero; March 15th, 2010 at 11:35 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
sum function pcase XSLT 2 January 2nd, 2008 05:51 PM
sum() function felixm_jr Reporting Services 1 April 22nd, 2007 01:59 AM
Sum function accuracy problem nuthib Crystal Reports 1 June 21st, 2004 02:12 AM
sum function problem sherr8 Access 1 February 13th, 2004 03:06 PM
Need Help with the Sum Function athanatos XSLT 1 July 22nd, 2003 10:06 AM





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