xsl-list
[Top] [All Lists]

Re: XSLT - Multiple Level Grouping with Unknown Level

2004-09-09 14:28:28
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/



<Prev in Thread] Current Thread [Next in Thread>