RE: [xsl] Generating Table Cell values to match Columns

Subject: RE: [xsl] Generating Table Cell values to match Columns
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Tue, 18 Mar 2003 17:43:45 -0500
[ Glenn Copen]
> 
> I have table data that may have blank entries for specific 
> cells. 

[snipped xml]
> How do I generate the <TD> element for the matching column in XSLT?  I
> need to somehow test the value of "colname" in <entry> against the
> "colname" in the <colspec>.
> 
> This needs to be a "generic" solution based on the XML data, 
> since I have
> many tables in the file with different number of columns, different
> colname values, etc.

As long as all column names and entries are unique, it is easy.  Here is
a solution using xsl:for-each to generate the header row and then to
work through each row.  For each column name, the xpath expression finds
the cell that matches the name, if there is one.  (I have omitted some
of the HTML elements for simplicity, but the result is still valid
HTML).

You may want to fancy it up  some, possibly testing for missing content,
but this should get you started.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method='html'/>

<xsl:variable name='column-names'
select='table/thead/colspec/@colname'/>

<xsl:template match="/table">
<table border='1'>
   <tr>
      <xsl:for-each select='$column-names'>
         <th><xsl:value-of select='.'/></th>
      </xsl:for-each>
   </tr>
   <xsl:apply-templates select='tbody/row'/>
</table>
</xsl:template>

<xsl:template match='row'>
   <xsl:variable name='entries' select='entry'/>
   <tr>
      <xsl:for-each select='$column-names'>
         <td><xsl:value-of select='$entries[@colname =
current()]'/></td>
      </xsl:for-each>
   </tr>
</xsl:template>

</xsl:stylesheet>

Cheers,

Tom P

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


Current Thread