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

Re: XSLT - Multiple Level Grouping with Unknown Level

Subject: Re: XSLT - Multiple Level Grouping with Unknown Level
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 9 Sep 2004 22:28:28 +0100
xsl first row
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/

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.