Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 6th, 2004, 05:54 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

Reply With Quote
  #2 (permalink)  
Old April 6th, 2004, 06:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #3 (permalink)  
Old April 6th, 2004, 06:49 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mate,

Great help!

Cheers,

Brett

Reply With Quote
  #4 (permalink)  
Old April 6th, 2004, 07:21 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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,

Reply With Quote
  #5 (permalink)  
Old April 6th, 2004, 07:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
  #6 (permalink)  
Old April 6th, 2004, 08:01 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 !

Reply With Quote
  #7 (permalink)  
Old April 6th, 2004, 08:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting on date column in XSLT Palwi XSLT 1 March 18th, 2008 08:19 AM
Sorting in XSLT hchaudh1 XSLT 1 November 30th, 2005 05:37 AM
How to get the value of pre? Edward King Javascript How-To 2 June 28th, 2005 08:38 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 02:20 AM
pre-processing chenggong Crystal Reports 0 July 21st, 2003 10:34 PM



All times are GMT -4. The time now is 06:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.