xsl-list
[Top] [All Lists]

Re: [xsl] Grouping and Numbering

2007-05-25 21:29:02
Thanks David for letting us know a nice solution.

I have added it on my site:
http://gandhimukul.tripod.com/xslt/extensions.html (item no. 2)

On 5/26/07, David Carlisle <davidc(_at_)nag(_dot_)co(_dot_)uk> 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="http://www.w3.org/1999/XSL/Transform";>
 <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:for-each>
     <xsl:if test="position()!=last()">
       <output  row="{$c+count(key('g',.))+$p+1}"/>
     </xsl:if>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>


 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>

David



________________________________________________________________________
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--