Re: [xsl] Grouping adjacent columns in a table

Subject: Re: [xsl] Grouping adjacent columns in a table
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Fri, 15 Jan 2010 15:27:19 +0000
That's an odd requirement but this should be enough to get you started:

<xsl:template match="/">
  <xsl:variable name="input" select="."/>
  <xsl:variable name="cols" as="element()+">
    <xsl:for-each select="1 to count(//tr[1]/td)">
      <col pos="{.}"><xsl:value-of select="$input//tr/td[current()]"/></col>
    </xsl:for-each>
  </xsl:variable>

  <xsl:for-each-group select="$cols" group-by=".">
    <th><xsl:value-of select="string-join(current-group()/@pos, ', ')"/></th>
  </xsl:for-each-group>
<xsl:template>

It stores the columns values in a variable:

<col pos="1">100 400 600 900</col>
<col pos="2">100 500 600 900</col>
<col pos="3">200 500 700 900</col>
<col pos="4">200 500 700 900</col>
<col pos="5">300 500 800 900</col>

then its easy enough to group them to find columns with all the same values:

<th>1</th>
<th>2</th>
<th>3, 4</th>
<th>5</th>

Given that, it should be enough to work out how to do the rest,

cheers
andrew


2010/1/15 Kevin Bird <kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx>:
> Hello
>
> I have an XHTML table which can have an arbitrary number of columns and
rows.
>
> For display purposes, I need to reduce the number of columns by grouping
adjacent duplicate columns.
>
> Therefore.
>
> <table border="1" rules="all">
>        <thead>
> <tr>
>        <th>A</th>
>        <th>B</th>
>        <th>C</th>
>        <th>D</th>
>        <th>E</th>
> </tr>
>        </thead>
>        <tbody>
> <tr>
>        <td>100</td>
>        <td>100</td>
>        <td>200</td>
>        <td>200</td>
>        <td>300</td>
> </tr>
> <tr>
>        <td>400</td>
>        <td>500</td>
>        <td>500</td>
>        <td>500</td>
>        <td>500</td>
> </tr>
> <tr>
>        <td>600</td>
>        <td>600</td>
>        <td>700</td>
>        <td>700</td>
>        <td>800</td>
> </tr>
> <tr>
>        <td>900</td>
>        <td>900</td>
>        <td>900</td>
>        <td>900</td>
>        <td>900</td>
> </tr>
>        </tbody>
> </table>
>
> becomes
>
> <table border="1" rules="all">
>        <thead>
> <tr>
>        <th>A</th>
>        <th>B</th>
>        <th>C,D</th>
>        <th>E</th>
> </tr>
>        </thead>
>        <tbody>
> <tr>
>        <td>100</td>
>        <td>100</td>
>        <td>200</td>
>        <td>300</td>
> </tr>
> <tr>
>        <td>400</td>
>        <td>500</td>
>        <td>500</td>
>        <td>500</td>
> </tr>
> <tr>
>        <td>600</td>
>        <td>600</td>
>        <td>700</td>
>        <td>800</td>
> </tr>
> <tr>
>        <td>900</td>
>        <td>900</td>
>        <td>900</td>
>        <td>900</td>
> </tr>
>        </tbody>
> </table>
>
> as columns 'C' and 'D' contain duplicate values. The real data could contain
up to 50 columns and grouping could span several columns.
>
> I can utilise a XSLT 2.0 solution.
>
> Any help you be appreciated.
>
> --
> Kevin
>
>



--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread