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 October 23rd, 2009, 03:59 PM
Registered User
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble with sum() and position()

I'm trying to sum up values by groups of 5 using a recursive call template, but something strange is happening.

Here is my input document:
Code:
<LOB full="Happyguard">
			<day date="10/05/09" lob="Happyguard" totalFailed="13"></day>
			<day date="10/06/09" lob="Happyguard" totalFailed="11"></day>
			<day date="10/07/09" lob="Happyguard" totalFailed="8"></day>
			<day date="10/08/09" lob="Happyguard" totalFailed="33"></day>
			<day date="10/09/09" lob="Happyguard" totalFailed="45"></day>
			<day date="10/12/09" lob="Happyguard" totalFailed="9"></day>
			<day date="10/13/09" lob="Happyguard" totalFailed="1"></day>
			<day date="10/14/09" lob="Happyguard" totalFailed="78"></day>
			<day date="10/15/09" lob="Happyguard" totalFailed="42"></day>
			<day date="10/16/09" lob="Happyguard" totalFailed="72"></day>
			<day date="10/19/09" lob="Happyguard" totalFailed="11"></day>
			<day date="10/20/09" lob="Happyguard" totalFailed="38"></day>
			<day date="10/21/09" lob="Happyguard" totalFailed="42"></day>
			<day date="10/22/09" lob="Happyguard" totalFailed="0"></day>
		</LOB>
This is the transformation:
Code:
<xsl:template match="/">
		<xsl:for-each select="LOB">
			<xsl:call-template name="aggregateWeek"/>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="aggregateWeek">
		<xsl:param name="startPosition" select="1"/>
		<xsl:variable name="countDays" select="count(day)"/>
		<xsl:variable name="endPosition">
			<xsl:choose>
				<xsl:when test="$countDays gt ($startPosition + 4)">
					<xsl:value-of select="($startPosition + 4)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$countDays"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="week">
			<xsl:attribute name="lob" select="@full"/>
			<xsl:attribute name="startDate" select="day[position()=$startPosition]/@date"/>
			<xsl:attribute name="endDate" select="day[position()=$endPosition]/@date"/>
			<xsl:attribute name="totalFailed" select="sum(day[position() ge $startPosition][position() le xs:integer($endPosition)]/@totalFailed)"/>
		</xsl:element>
		<xsl:if test="xs:integer($endPosition) lt $countDays">
			<xsl:call-template name="aggregateWeek">
				<xsl:with-param name="startPosition" select="($startPosition + 5)"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>
Here is the output I'm getting:
Code:
<week startDate="10/05/09" lob="Happyguard" totalFailed="110" endDate="10/09/09"></week>
		<week startDate="10/12/09" lob="Happyguard" totalFailed="293" endDate="10/16/09"></week>
		<week startDate="10/19/09" lob="Happyguard" totalFailed="91" endDate="10/22/09"></week>
Notice week 2 is not correct. Nodes 1-5 sum correctly. Nodes 11-14 sum correctly. But with week 2, it's almost as if it's summing from nodes 6-14. Can anyone shed light onto what is happening here?

Thanks
 
Old October 23rd, 2009, 04:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try replacing

Code:
day[position() ge $startPosition][position() le xs:integer($endPosition)]
by

Code:
day[position() ge $startPosition and position() le xs:integer($endPosition)]
In the second predicate, position() refers to the position of a node in the sequence after applying the first predicate, not the position in the original sequence.

Alternatively, write

Code:
day[position() = ($startPosition to $endPosition)]
And I would suggest adding
Code:
as="xs:integer"
to the relevant xsl:variable and xsl:param declarations.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
POSITION() pallone XSLT 8 June 3rd, 2009 10:04 AM
How to get the position of the mouse sbkrohit CSS Cascading Style Sheets 4 November 5th, 2008 04:56 PM
Position barski XSLT 5 July 11th, 2007 01:54 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Trouble with Sum sefrank Classic ASP Databases 1 January 14th, 2004 08:15 AM





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