Re: [xsl] Grouping and Numbering

Subject: Re: [xsl] Grouping and Numbering
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 26 May 2007 09:58:37 +0530
Thanks David for letting us know a nice solution.

I have added it on my site: (item no. 2)

On 5/26/07, David Carlisle <davidc@xxxxxxxxx> wrote:

> I find this problem rather difficult to solve with pure XSLT 1.0. This > is due to the fact, that XSLT doesn't allow to maintain variable > state

It's easy to do it in xslt1 using two stylesheets or 1 stylesheet and the xx:nde-set() extension: first do the grouping and then do a second pass that adds the numbering (for which you can just use position() or xsl:number/> once the complication of sorting has beenresolved. It is presumably possible to do it in one xslt1 stylesheet without extensions but I suspect you'd have to be quite inefficient repeatedly calculating the sort order, and apart from mozilla pretty much all xslt1 engines have a node-set extension.

oh what the heck I suppose I'd better do an xslt1 version, with no extensions...

<xsl:stylesheet version="1.0" xmlns:xsl="";> <xsl:key name="g" match="group_id" use="." /> <xsl:output indent="yes"/>

<xsl:variable name="g" select="/sample/result/details/group_id[generate-id()=generate-id(key('g',.))]"/>

 <xsl:template match="sample">
   <xsl:for-each select="$g">
     <xsl:sort select="."/>
     <xsl:variable name="p" select="position()-1"/>
     <xsl:variable name="c" select="count(key('g',$g[.&lt;current()]))"/>
     <xsl:for-each select="key('g',.)">
       <output row="{$c+$p+position()}"><xsl:value-of select="."/></output>
     <xsl:if test="position()!=last()">
       <output  row="{$c+count(key('g',.))+$p+1}"/>


saxon sg.xml sg.xsl <?xml version="1.0" encoding="utf-8"?> <output row="1">250</output> <output row="2">250</output> <output row="3"/> <output row="4">300</output>


The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.

-- Regards, Mukul Gandhi

Current Thread