Re: [xsl] Grouping and Numbering

Subject: Re: [xsl] Grouping and Numbering
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 25 May 2007 21:17:25 +0100
> 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. 
________________________________________________________________________

Current Thread