[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XSLT - Multiple Level Grouping with Unknown Level


Subject: Re: [xsl] XSLT - Multiple Level Grouping with Unknown Level
From: "Arun Sinha" <arunsinha666@xxxxxxxxxxx>
Date: Fri, 10 Sep 2004 07:13:55 +0100

Hi Jeni,

Thanks a lot.

You are a STAR.

Actually the XML document deosn't have <Groups> element at the top level.
I will ask my colleagues in UK to incorporate the same. I am based in India.

I am getting the XML file from UK and my responsibility is to write stylesheet.

Even though I stayed in UK for 4 years my English is still not upto mark. :-)
When I said 'fixed level' that meant the number of levels are not known.


I did the coding for 2 levels of grouping after finding the solution on your website.
I would appreciate if you could put this one, as well, on your website.


Cheers.

Arun

From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: "Arun Sinha" <arunsinha666@xxxxxxxxxxx>
CC: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT - Multiple Level Grouping with Unknown Level
Date: Thu, 9 Sep 2004 22:28:28 +0100

Hi Arun,

> I have just started doing coding in XSLT and stuck with a unique
> problem. I have done the coding for grouping with fixed levels but
> finiding the solution for this one is getting too complicated.

I'll give this a shot, but I'm not sure what you mean by "fixed
levels", so I'm not certain that this will do what you want.

You're lucky in that your source actually already contains the groups
that you need: usually they don't, and people have to manufacture the
groups using keys etc.

I've assumed that you've got a <Groups> element wrapped around the
top-level <Group> elements. Create a table at that level, and insert
the relevant column headers there. Then apply templates to all the
<Row> elements within the <Groups> element: we know we need one row in
the table for each <Row> in the source.

<xsl:template match="Groups">
  <table>
    <thead>
      ...
    </thead>
    <tbody>
      <xsl:apply-templates select=".//Row" />
    </tbody>
  </table>
</xsl:template>

So we create one row per <Row> in the source. Keep track of the ID of
the row: I'm using generate-id() here, but perhaps your source
actually has a unique identifier that you could use. To fill in the
first set of columns, you need to get the values from the <GroupValue>
elements of the <Group> elements in which the <Row> appears. But you
only want to output them if the row you're on is the first row within
the group. So locate that first row and get its ID ($first-row-id),
and only output the <GroupValue> if that ID matches the one for the
current row.

<xsl:template match="Row">
<xsl:variable name="row-id" select="generate-id(.)" />
<tr>
<xsl:for-each select="ancestor::Group">
<td>
<xsl:variable name="first-row-id" select="generate-id(descendant::Row[1])" />
<xsl:if test="$row-id = $first-row-id">
<xsl:value-of select="GroupValue" />
</xsl:if>
</td>
</xsl:for-each>
<xsl:apply-templates select="ColumnValue" />
</tr>
</xsl:template>


The rest of the cells are simply the values from the <ColumnValue>
elements: that's very straight-forward:

<xsl:template match="ColumnValue">
  <td><xsl:value-of select="." /></td>
</xsl:template>

Cheers,

Jeni

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


_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today! http://www.msn.co.uk/messenger



Current Thread
Keywords