[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Sorting unsorted elements based on their attribute

Subject: RE: Sorting unsorted elements based on their attributes
From: Américo Albuquerque <melinor@xxxxxxx>
Date: Wed, 4 Jun 2003 20:09:01 +0100
sorting on xml attributes
Hi.

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of thei
> Sent: Wednesday, June 04, 2003 5:31 PM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  Sorting unsorted elements based on their attributes
> 
> 
> Hello,
> 
> I have an xml file as thus:
> 
> <comments>
> <comment page="20030101">blah</comment>
> <comment page="20030207">blah</comment>
> <comment page="20011127">blah</comment>
> ...etc
> </comments>
> 
> Each <comment> element has a page attribute, which contains a
> date in format CCYYMMDD as shown above.
> 
> I'm trying to use a XSL stylesheet to display these, or sort
> them if you prefer that wording, into a year/month/day type 
> format. In other words, if there's say 12 comments for 
> 2003-06-05, then I want to display all 12 in a list under the 
> heading 2003-06-05, then there may be another 4 comments for 
> 2003-06-04, which I want to display under their heading, etc. 
> I can do the displaying and all easily enough, but I'm having 
> trouble sorting the results into days and months.
> 

This is a grouping problem, see
http://www.jenitennison.com/xslt/grouping.html for more information.

Meanwhile try this:

 <xsl:key name="comments" match="comment" use="@page"/>
 
 <xsl:template match="comments">
  <xsl:apply-templates
select="comment[generate-id()=generate-id(key('comments',@page))]"
mode="headers"/>
 </xsl:template>
 
 <xsl:template match="comment">
  <xsl:apply-templates/>
  <xsl:text>&#10;</xsl:text>
 </xsl:template>
 
 <xsl:template match="comment" mode="headers">
  <xsl:value-of select="substring(@page,1,4)"/>
  <xsl:text>-</xsl:text>
  <xsl:value-of select="substring(@page,5,2)"/>
  <xsl:text>-</xsl:text>
  <xsl:value-of select="substring(@page,7,2)"/>
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates select="key('comments',@page)"/>
 </xsl:template>

Hope this helps you



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.