RE: [xsl] questions. (urgent)

Subject: RE: [xsl] questions. (urgent)
From: "Kelvin Law" <kelvin.law@xxxxxxxxxxx>
Date: Fri, 30 May 2003 18:05:06 -0400
Hi,
	I am fairy new to XSL, so I am not sure if I am doing this right or not.
Correct me if I am wrong ;)

After couple hours of reading on the web, I got a partial solutions to my
problem.

Basically it is based on the Muenchian Method with recursion for calculating
total for the sum.


<xsl:template match="person" mode="sumByType">
	<xsl:param name="total" select="0"/>
	<xsl:variable name="n" select="concat(date,'+',type)"/>

	<xsl:choose>
	   <xsl:when test="not(following-sibling::*[position()=1 and
(concat(date,'+', paymentType)=$n)])">
				<xsl:value-of select="$total+amount"/><br/>
	   </xsl:when>
	   <xsl:otherwise>
		<xsl:apply-templates mode="sumByType"
		  select="following-sibling::*[position()=1 and (concat(date,'+',
paymentType)=$n)]">
			<xsl:with-param name="total" select="$total+amount"/>
		</xsl:apply-templates>
	   </xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="person" mode="sumByDate">
	<xsl:param name="total" select="0"/>
	<xsl:variable name="n" select="date"/>

	<xsl:choose>
	   <xsl:when test="not(following-sibling::*[position()=1 and (date=$n)])">
				<xsl:value-of select="$total+amount"/>
	   </xsl:when>
	   <xsl:otherwise>
		<xsl:apply-templates mode="sumByDate"
		  select="following-sibling::*[position()=1 and (date=$n)]">
			<xsl:with-param name="total" select="$total+amount"/>
		</xsl:apply-templates>
	   </xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:key name="key1" match="person" use="date"/>
<xsl:key name="key2" match="person" use="concat(date, '+', paymentType)"/>



<!-- First Level  = date -->
<!-- Second Level = paymentType	    -->


<!-- First Level -->
<xsl:template match="person" mode="firstLevel">
	<xsl:value-of select="trnsDate" /></b>

	<!-- calling Second Level -->
	<xsl:apply-templates mode="secondLevel"
		select="key('key1', paymentType)
					[generate-id(.) =
					 generate-id(key('key2',
								   concat(date, '+', paymentType)))]">
		<!-- by userName -->
		<xsl:sort select="paymentType"/>
	</xsl:apply-templates>

	Total for <xsl:value-of select="date"/>
	<xsl:apply-templates mode="sumByDate" select="key('key1',date)[1]">
	</xsl:apply-templates>
</xsl:template>
<!-- end First Level -->

<!-- Second Level -->
<xsl:template match="person" mode="secondLevel">
	<xsl:value-of select="type" />

	<!-- calling Third Level -->
	<xsl:apply-templates mode="thirdLevel"
		select="key('key2', concat(date,'+', paymentType))">
		<xsl:sort select="name"/>
	</xsl:apply-templates>

	<xsl:value-of select="type" /> Total

	<xsl:apply-templates mode="sumByType"
			select="key('key2',concat(date, '+', paymentType)[1]">
	</xsl:apply-templates>
</xsl:template>
<!-- end Second Level -->

<!-- Third Level -->
<xsl:template match="person" mode="thirdLevel">
	<xsl:value-of select="name"/>
	<xsl:value-of select="amount"/>
</xsl:template>
<!-- end Third Level -->

<!-- END XSL Template -->


<xsl:for-each select="persons">
	<xsl:apply-templates select="persons[generate-id(.) =
						generate-id(key('key1', date)[1])]"
						mode="firstLevel"/>
	Total For Group <xsl:copy-of select="sum(person/amount)" />
</xsl:for-each>


Phew, there you have it.

The only problem with this xsl is that if the data in the xml is not in
order, the summation won't work.
Because it is depending on the following-sibling.

for example if I have something like:
...
	<person>
		<name>David</name>
		<amount>15.00</amount>
		<date>05/13/2003</date>
		<paymentType>CASH</paymentType>
	</person>
	<person>
		<name>Ada</name>
		<amount>20.00</amount>
		<date>05/10/2003</date>
		<paymentType>DEBIT CARD</paymentType>
	</person>
	<person>
		<name>Kenny</name>
		<amount>20.00</amount>
		<date>05/13/2003</date>
		<paymentType>CHEQUE</paymentType>
	</person>
...

The sum for 05/13/2003 will NOT equal to $35.00 because the
following-sibling is in a different group which breaks the recursion.
	Of course, this is not a desirable solutions.

So could someone help me out with this?? So that my XSL doesn't depends on
the order of the elements in the XML?

Thanks in advance.


Kelvin



-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of
Jarno.Elovirta@xxxxxxxxx
Sent: Friday, May 30, 2003 1:47 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] questions. (urgent)


Hi,

[snip]

> I have already got the grouping working correctly with a
> solution derived
> from:
> 	http://www.dpawson.co.uk/xsl/sect2/N4486.html#d4085e391
>
> However, I can't get the TOTAL working.
> Can someone please share some light on how I can get the
> total (the one with
> (*)) using XSLT?

Could you show us the stylesheet you have so far?

Cheers,

Jarno - E-Craft: Kill the Fakes (Conquer the States Mix)

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 5/6/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 5/6/2003


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread