[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: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 25 May 2007 19:16:30 -0400
Re:  Grouping and Numbering
Good literature deserves commentary --

At 04:17 PM 5/25/2007, David wrote:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="g" match="group_id" use="." />

The 'g' key matches group-id elements by their value, so for example key('g','250') returns all group-id elements with value '250' (however many there are). This is the essence of a Muenchian group.


<xsl:output indent="yes"/>

For cosmetics.


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

This selects a set of elements, binding them to $g. The set is all the group-id elements whose unique identifier (returned by generate-id()) has the same value as the unique id of the first element returned from the 'g' key using their value (following the XSLT 1.0 rule that the generated id of a set of nodes will be that of the first node in the set, in document order). That is, it returns the first group-id element in the document that appears with any given value -- so all values are returned exactly once.


You can call these "flag-bearer" elements if you like, since they correspond exactly to the groups of group-id elements, using their values.

  <xsl:template match="sample">
    <xsl:for-each select="$g">

Iterating over the flag-bearers is equivalent to iterating over the groups themselves (since each can be used to retrieve its group).


<xsl:sort select="."/>

Dr Carlisle sorts the groups' flag-bearers by their values, so the groups will come out in ascending order by value.


<xsl:variable name="p" select="position()-1"/>

This returns us '0' for the first group, 'n-1' for the nth group, in their sorted order.


<xsl:variable name="c" select="count(key('g',$g[.&lt;current()]))"/>

This returns us the count of the members of all the groups that will appear before this one (a tricky business), by finding all the flag-bearers whose value is less than the current group's, collecting all the members of their groups, and counting them. The key makes this reasonably efficient to do -- but it wouldn't work if we weren't outputting the groups ordered by their values (he'd have to come up with something else).


<xsl:for-each select="key('g',.)">

Now we iterate over the members of the group belonging to this flag-bearer.


<output row="{$c+$p+position()}"><xsl:value-of select="."/></output>

We write an 'output' element. The value of its @row is calculated as: * The count of all the members of the groups preceding this one (since they'll appear above this one) * The number of blank lines we'll also include, which happens to be $p * The position of this member of the group within the group

Its value is its value.

</xsl:for-each>

We're done with the members of this group.


<xsl:if test="position()!=last()">

But unless we're the last group (or rather: the last flag-bearer), we also want...


<output row="{$c+count(key('g',.))+$p+1}"/>

... a blank 'output' element whose row is calculated as the sum of all the members of the groups preceding this one, all the members of this group, the blank lines preceding this one, and 1 (for this blank line)


      </xsl:if>
    </xsl:for-each>
  </xsl:template>

We're done.


Have a good weekend.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

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.