[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Grouping by attribute values


Subject: RE: [xsl] Grouping by attribute values
From: "Jim Neff" <jneff@xxxxxxxxxxxxxxx>
Date: Tue, 1 Mar 2005 10:42:10 -0500

Hi Josh,

I had fun figuring this one out.  I'm not very good at this grouping stuff
but it DOES produce the desired output (and an instructor once told me that
getting the proper results is 70% of the grade).

The only problem I can see with my solution is that there MUST always be the
same limits for every premium within a class.  So if a premium does not have
the same limit attribute as the others within the class then this will not
work.  In the time I have for this I couldn't figure out how to group
columns by limit (think pivot table).

Also, I am using the for-each-group instruction which is an XSLT version 2
syntax.  I'm not smart enough to understand Muenchian grouping but if you
are restricted to version 1, then you should be able to figure out a way to
emulate the version 1 grouping based on my version  2 example here.

Just FYI, if I had to do this I would massage the data into a more
XSL-friendly format before trying to output it for display.  I would use a
<xsl:variable> at the top of the document and reorganize the data so that I
looks more like the format in your question and then use basic templates on
that new structure.

<ratePremiums>

	<class id="1">

		<driving_record value="0">

			<limit value="200">

				<premium>820.7014415999998</premium>

			</limit>

			<limit value="300">

				<premium>853.5294992639998</premium>

			</limit>

		</driving_record>

		...

	</class>

	...

</ratePremiums>

Good luck  ;-)

--Jim Neff


<xsl:stylesheet 
	version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	>

	<xsl:output method="html" indent="yes"/>
	
	<xsl:template match="ratePremiums">
		<html>
		
			<body>
	
				<xsl:apply-templates select="coverage" />
	
			</body>
			
		</html>
	</xsl:template>
	
	<xsl:template match="coverage">

		<xsl:for-each-group select="premium" group-by="@class" > 
		<p> class = <xsl:value-of select="current-grouping-key()" />
</p>
		<table>
	
			<tr>
		
				<th>
			
					<!-- first one is blank --> 
				
				</th>
		
				<xsl:for-each-group select="current-group()"
group-by="@limit">
				
					<th>
					
						<xsl:value-of
select="current-grouping-key()" />
					
					</th>

				
				</xsl:for-each-group>
						
			</tr>
		
			
			<xsl:for-each-group select="current-group()"
group-by="@drivingRecord">
			
				<tr>
			
					<td>
						
						<xsl:text>DR = </xsl:text>
						<xsl:value-of
select="@drivingRecord" />
						
					</td>
				
					<xsl:for-each
select="current-group()">
					
						<td>
						
							<xsl:value-of
select="." />
						
						</td>
					
					</xsl:for-each>

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

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


> -----Original Message-----
> From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx] 
> Sent: Tuesday, March 01, 2005 8:50 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] Grouping by attribute values
> 
> Go to this URL 
> (http://techrepublic.com.com/5046-22-0.html?SearchThis=Muenchi
> an+grouping&nodeIds=all&go=GO) and get this document: - 
> Overcoming decades old legacy systems with XSLT and Muenchian 
> grouping .
> --
> Charles Knell
> cknell@xxxxxxxxxx - email
> 
> 
> 
> -----Original Message-----
> From:     Josh Taylor <jktylr@xxxxxxxxxxx>
> Sent:     Tue, 01 Mar 2005 09:38:16 -0400
> To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  [xsl] Grouping by attribute values
> 
> I have the following problem. My XML is structured in the 
> following way:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <ratePremiums rateDate="20041001" territory="100" type="1">
> <coverage id="Liability">
>   <premium class="1" drivingRecord="0" 
> limit="200">820.7014415999998</premium>
>   <premium class="1" drivingRecord="0" 
> limit="300">853.5294992639998</premium>
>   <premium class="1" drivingRecord="1" 
> limit="200">805.7559679999999</premium>
>   <premium class="1" drivingRecord="1" 
> limit="300">837.9862067199999</premium>
> ....
>   <premium class="12" drivingRecord="2" limit="200">0.0</premium>
>   <premium class="12" drivingRecord="2" limit="300">0.0</premium>
> </coverage>
> </ratePremiums>
> 
> I need to display premiums grouped first by class, then by 
> driving record 
> and then by limit in
> HTML tables.
> 
> So a table for each class, a row for each driving record and 
> a column for 
> each limit (much like an engineering table).
> 
> To elaborate: Desired output for the snipet above is:
> Class 1:
> <table>
>   <tr><th></th><th>200 K</th><th>300 K</th></tr>
>   <tr><td>DR = 
> 0</td><td>820.7014415999998</td><td>853.5294992639998</td></tr>
>   <tr><td>DR = 
> 0</td><td>805.7559679999999</td><td>837.9862067199999</td></tr>
>   ....
> </table>
> ... etc.
> 
> I have no idea how to loop through the premiums, isolated a 
> unique class to 
> create a table. Then for that class loop through unique 
> driving record 
> values and display the premiums according to limit.
> 
> I do not want to structure my XML with this table view in mind.
> 
> Thank you for your time!
> 
> - Josh Taylor


Current Thread
Keywords