 |
| 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
|
|
|
|

April 6th, 2004, 04:54 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
pre-sorting in XSLT - help!
Hi,
I am fairly new to XSLT and have an annoying problem which i simply can't solve. It involves picking the "Best in each group".
For example:
<garage>
<car model="ford" year="1987"/>
<car model="mustang" year="1990"/>
<car model="ford" year="1999"/>
<car model="bmw" year="1992"/>
<car model="mustang" year="1998"/>
<car model="ford" year="2000"/>
</garage>
I want a transformation to make a simple html table which has the NEWEST ford, mustang and bmw. ie.
Model | Newest
-----------------
ford | 2000
mustang | 1998
bmw | 1992
My idea was to split the data into groups (which seems easy but only in XSLT2 which i don't have!), then to sort it by date, then to choose the first line in each group and collate it. However - I stubble when sorting it as I cannot "collect" the output from the sort as a node set.
Any Ideas?
|
|

April 6th, 2004, 05:27 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Try this slight variation on the standard Muenchian Grouping method:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="CarsByModel" match="car" use="@model"/>
<xsl:template match="/">
<html>
<head>
<title>Muenchian Grouping Example</title>
</head>
<body>
<h3>Muenchian Grouping Example</h3>
<xsl:apply-templates select="garage/car[generate-id()=generate-id(key('CarsByModel', @model)[1])]">
<xsl:sort select="@model" order="ascending"/>
<xsl:sort select="@year" order="ascending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="car">
<xsl:value-of select="@model"/> |
<xsl:for-each select="key('CarsByModel', @model)">
<xsl:if test="position()=last()">
<xsl:value-of select="@year"/><br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
hth
Phil
|
|

April 6th, 2004, 05:49 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Mate,
Great help!
Cheers,
Brett
|
|

April 6th, 2004, 06:21 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OK here we go again! My problem was a little more complicated than that and i prob should not have simplified. The structure of the xml document (in the context of the above example), uses more then one garage. ie:
<garage>
<car ...
<car ...
<garage/>
<garage>
...
<garage/>
<garage>
...
<garage/>
etc...
and so the method you specified unfortunaley only works for the top "garage". I have a for-each call near the top of the document which covers all the garages. How to I make a key for each garage in turn?
Cheers,
|
|

April 6th, 2004, 06:55 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You don't need or want a separate key for each garage. If your xml is slightly different to that which you posted then you just need to change the key definition accordingly. For example if you had this xml:
Code:
<garages>
<garage>
<car model="ford" year="1999"/>
<car model="bmw" year="1992"/>
<car model="mustang" year="1998"/>
</garage>
<garage>
<car model="ford" year="1987"/>
<car model="mustang" year="1990"/>
</garage>
<garage>
<car model="ford" year="2000"/>
</garage>
</garages>
then you would just need to change your key definition to this (note I've also moved the sort on @year - makes more sense to be where it is now...):
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="CarsByModel" match="garage/car" use="@model"/>
<xsl:template match="/">
<html>
<head>
<title>Muenchian Grouping Example</title>
</head>
<body>
<h3>Muenchian Grouping Example</h3>
<xsl:apply-templates select="garages/garage/car[generate-id()=generate-id(key('CarsByModel', @model)[1])]">
<xsl:sort select="@model" order="ascending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="car">
<xsl:value-of select="@model"/> |
<xsl:for-each select="key('CarsByModel', @model)">
<xsl:sort select="@year" order="ascending"/>
<xsl:if test="position()=last()">
<xsl:value-of select="@year"/><br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
rgds
Phil
|
|

April 6th, 2004, 07:01 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ah, I see what you are getting at. However I am after the table for each garage - so it wouldn't be the newest year for each model of all the cars, it would be for each garage:
So the output would be a table for each garage in turn!
Model | Newest
-----------------
ford | 2003
mustang | 1995
bmw | 1991
Model | Newest
-----------------
ford | 2002
mustang | 1991
bmw | 1993
Model | Newest
-----------------
ford | 2000
mustang | 1998
bmw | 1992
Sorry to be a pain !
|
|

April 6th, 2004, 07:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
OK, in that case its much easier not to use keys since they are only allowed at the top-level. Use something like this which utilises preceding-sibling axis so as a result its not very efficient for a large xml document, but anyway try it out.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="garages/garage"/>
</xsl:template>
<xsl:template match="garage">
<table border="1">
<xsl:for-each select="car[not(@model = preceding-sibling::car/@model)]">
<xsl:apply-templates select="../car[@model = current()/@model]">
<xsl:sort select="@year" order="ascending"/>
</xsl:apply-templates>
</xsl:for-each>
</table><br/><br/>
</xsl:template>
<xsl:template match="car">
<xsl:if test="position()=last()">
<tr>
<td><xsl:value-of select="@model"/></td><td><xsl:value-of select="@year"/></td>
</tr>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
you've now used up all your support tokens for this problem ;)
rgds
Phil
|
|
 |