Re: [xsl] grouping by attribute

Subject: Re: [xsl] grouping by attribute
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 10 Oct 2001 09:25:31 +0100
Hi Kiran,

>From the sample output that you gave, it appears that you want to
group the Amenity elements *by position* (in groups of three) rather
than *by value* (which is what 'by attribute' seems to imply, and what
you were trying with the code that you got from the FAQ). The relevant
FAQ is currently at
http://www.dpawson.co.uk/xsl/sect2/N4486.html#d137e225 (Grouping by
position).

You should process the 1st, 4th, 7th etc. Amenity in the list to
create the row. You can find these Amenity elements by looking at
their position mod 3 to see if it's equal to 1:

  <xsl:for-each select="hotel:Amenity[position() mod 3 = 1]">
    <tr>
      ...
    </tr>
  </xsl:for-each>

To get the cells, you want to give the value of the Description
attribute of the Amenity that you're looking at and its two following
siblings. You can fix the number of columns that you use, if you want,
and do:

  <xsl:for-each select="hotel:Amenity[position() mod 3 = 1]">
    <tr>
      <td><xsl:value-of select="@Description" /></td>
      <td>
        <xsl:value-of
          select="following-sibling::hotel:Amenity[1]/@Description" />
      </td>
      <td>
        <xsl:value-of
          select="following-sibling::hotel:Amenity[2]/@Description" />
      </td>
    </tr>
  </xsl:for-each>

Or you can iterate over the next N Amenity elements, where N is the
number of columns that you want:

  <xsl:for-each select="hotel:Amenity[position() mod $cols = 1]">
    <tr>
      <xsl:for-each select=". |
                            following-sibling::hotel:Amenity
                              [position() &lt; $cols]">
        <td><xsl:value-of select="@Description" /></td>
      </xsl:for-each>
    </tr>
  </xsl:for-each>

The former (where you fix the number of columns) makes it easier to
include empty cells than the latter (where you only get a cell if
there's a description to put in it).
  
> Also I want to know, what difference does it make to use either of
> the following statements, on the execution of the rest of the code
> in XSL. I could see the solution for grouping 3 items (as I want it)
> in FAQ section, but that code does not work when I use the 1st
> sentence from the following statements. Plzz guide me regarding this
> too.
>
> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl";>
>
> or
>
> <xsl:stylesheet version="1.0"
>                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

Technically, the two stylesheets differ in terms of which namespace
they associate with the 'xsl' prefix. Practically, they're written in
different languages - the first is written in a Microsoft specific
dialect that's sometimes called 'XSL' and the second is written in the
W3C standard XSLT. You should be using the second namespace; see the
MSXML FAQ at http://www.netcrucible.com/xslt/msxml-faq.htm for more
details.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread