xsl-list
[Top] [All Lists]

RE: Sorting unsorted elements based on their attributes

2003-06-04 12:09:01
Hi.

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
thei
Sent: Wednesday, June 04, 2003 5:31 PM
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] 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



<Prev in Thread] Current Thread [Next in Thread>