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

RE: [xsl] Numbering in XSLT


Subject: RE: [xsl] Numbering in XSLT
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 10 Jun 2008 22:59:34 +0100

Your first challenge here is the sorting. I imagine that 01/01/2007 is a
date, perhaps in dd/mm/yyyy format or perhaps mm/dd/yyyy. Sorting dates is
easier if you use the international standard format yyyy-mm-dd (it can
always be reformatted for presentation later - XSLT 2.0 has a format-date()
function for this). But if you must use this format, you can sort by
rearranging the date using <xsl:sort select="concat(substring(@date1, 7, 4),
substring(@date1,....))"/>.

The next challenge is grouping. This is much easier in XSLT 2.0 which has an
<xsl:for-each-group> instruction. If for some reason you're stuck with XSLT
1.0, google (or look in the index of your XSLT textbook) for "Muenchian
grouping". It looks complicated but you get the hang of it.

Finally, you have to number the groups. That's the easy bit. Whatever method
of grouping you use, there's likely to be an <xsl:for-each> instruction to
iterate over the groups, and within that, <xsl:value-of
select="position()"/> will give you a sequential number. Subtract one to
start at zero.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: shruta satam [mailto:the_last_angel_00@xxxxxxxxx] 
> Sent: 10 June 2008 19:55
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Numbering in XSLT
> 
> Hello All,
> 
> I am an absolute newbee to XSLT and have been cracking my 
> head trying to achieve the following:
> 
> Sample XML file:
> 
> <date>
> <ed date1="01/01/2007" id="1" />
> <ed date1="01/01/2008" id="2" />
> <ed date1="01/01/2008" id="3" />
> <ed date1="01/01/2005" id="4" />
> <ed date1="01/01/2008" id="5" />
> <ed date1="01/01/2009" id="6" />
> </date>
> 
> Now I want a numbered output as follows:
> 
> 0. 01/01/2005,4
> 1. 01/01/2007,1
> 2. 01/01/2008,2
> 2. 01/01/2008,3
> 2. 01/01/2008,5
> 3. 01/01/2009,6
> 
> i.e I want a numbered o/p grouped by date1 as shown above. Help!


Current Thread
Keywords