RE: [xsl] a*b, sum(c*d), sorted on <sort>

Subject: RE: [xsl] a*b, sum(c*d), sorted on <sort>
From: "McNally, David" <David.McNally@xxxxxxxxxx>
Date: Wed, 4 Dec 2002 12:23:13 -0500
As usual, there are many ways to handle this - here's one possibility.  Not
extensively tested....

Basically I for-each through the sorted items once to build a white-space
delimited string of unique ids.  I'm then going to do a sort of recursive
apply-templates - each time applying templates to the next item in my list,
and passing as parameters the total so far of c*d and the remaining ids in
the list.  I think this means I'm only iterating through the nodes twice,
rather than N-1 times, which is presumably good.

sortsum.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sortsum.xslt"?>
<root>
	<item>
		<a>1</a>
		<b>2</b>
		<c>2</c>
		<d>7</d>
		<sort>3</sort>
	</item>
	<item>
		<a>2</a>
		<b>3</b>
		<c>5</c>
		<d>8</d>
		<sort>2</sort>
	</item>
	<item>
		<a>3</a>
		<b>4</b>
		<c>1</c>
		<d>2</d>
		<sort>4</sort>
	</item>
	<item>
		<a>4</a>
		<b>5</b>
		<c>2</c>
		<d>9</d>
		<sort>1</sort>
	</item>
</root>

sortsum.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"/>

<xsl:template match="/">
	<table border="1" width="100%">
		<tbody>
		<tr>
<th>a</th><th>b</th><th>c</th><th>d</th><th>sort</th><th>a*b</th><th>c*d</th
><th>sum(c*d)</th>
		</tr>

		<xsl:variable name="nodelist">
			<xsl:for-each select="//item">
				<xsl:sort select="sort" data-type="number"/>
				<xsl:value-of
select="concat(generate-id(.),' ')"/>
			</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="firstnode"
select="substring-before($nodelist,' ')"/>
		<xsl:variable name="followingnodes"
select="substring-after($nodelist,' ')"/>

		<xsl:apply-templates select="//item[generate-id(.) =
$firstnode]">
			<xsl:with-param name="nodelist"
select="$followingnodes"/>
		</xsl:apply-templates>

		</tbody>
	</table>
</xsl:template>

<xsl:template match="item">
	<xsl:param name="totalsofar" select="0"/>
	<xsl:param name="nodelist"/>
	<tr>
		<th>
			<xsl:value-of select="a"/>
		</th>
		<th>
			<xsl:value-of select="b"/>
		</th>
		<th>
			<xsl:value-of select="c"/>
		</th>
		<th>
			<xsl:value-of select="d"/>
		</th>
		<th>
			<xsl:value-of select="sort"/>
		</th>
		<th>
			<xsl:value-of select="a*b"/>
		</th>
		<th>
			<xsl:value-of select="c*d"/>
		</th>
		<th>
			<xsl:value-of select="(c*d) + $totalsofar"/>
		</th>

		<xsl:variable name="firstnode"
select="substring-before($nodelist,' ')"/>
		<xsl:variable name="followingnodes"
select="substring-after($nodelist,' ')"/>

		<xsl:apply-templates select="//item[generate-id(.) =
$firstnode]">
			<xsl:with-param name="nodelist"
select="$followingnodes"/>
			<xsl:with-param name="totalsofar" select="(c*d) +
$totalsofar"/>
		</xsl:apply-templates>
		
	</tr>
</xsl:template>
	
</xsl:stylesheet>

And the result is:

a b c d sort a*b c*d sum(c*d) 
4 5 2 9 1 20 18 18 
2 3 5 8 2 6 40 58 
1 2 2 7 3 2 14 72 
3 4 1 2 4 12 2 74 

A very similar effect could probably be achieved using node-set.

David.
--
David McNally            Moody's Investors Service
Software Engineer        99 Church St, NY NY 10007 
David.McNally@xxxxxxxxxx            (212) 553-7475 



> -----Original Message-----
> From: Dan Diebolt [mailto:dandiebolt@xxxxxxxxx]
> Sent: Wednesday, December 04, 2002 10:53 AM
> To: xsl-list-digest@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] a*b, sum(c*d), sorted on <sort>
> 
> 
> Here is my XML:
> 
> <root>
>  <item><a>1</a><b>2</b><c>2</c><d>7</d><sort>3</sort></item>
>  <item><a>2</a><b>3</b><c>5</c><d>8</d><sort>2</sort></item>
>  <item><a>3</a><b>4</b><c>1</c><d>2</d><sort>4</sort></item>
>  <item><a>4</a><b>5</b><c>2</c><d>9</d><sort>1</sort></item>
> </root>
> 
> For each <item> I need to produce a row containing
> cells for a*b and cumulative c*d where the items
> are taken sorted on <sort>:
> 
>  +-----+----------+
>  | a*b | sum(c*d) |
>  +-----+----------+
>  |  20 |    18    |
>  +-----+----------+
>  |   6 |    58    |
>  +-----+----------+
>  |   2 |    72    |
>  +-----+----------+
>  |  12 |    74    |
>  +-----+----------+
> 
> The above a gross simplification, but I would like to get input from
> people on how they would structure the problem as it has a lot
> more detail in reality. In a nutshell, I have both recursive and 
> non-recursive calculations to perform in each row.
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


---------------------------------------

The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission.  If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited.  If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments.  Thank you.

Every effort is made to keep our network free from viruses.  You should, however, review this e-mail message, as well as any attachment thereto, for viruses.  We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.


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


Current Thread