[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Grouping and Numbering

Subject: Re: Grouping and Numbering
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 26 May 2007 09:58:37 +0530
Re:  Grouping and Numbering
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@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="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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.