Re: [xsl] How to calculate rowspan?

Subject: Re: [xsl] How to calculate rowspan?
From: "cking" <cking@xxxxxxxxxx>
Date: Thu, 26 Aug 2004 22:08:58 +0200
Hi Anton

> Unfortunatly they aren't unique... 

OK... I made a recursive template to calculate the rowspan.
This should work correctly, also in the AAABBA case!

 ... context of COL0 | COL1 | COL2 | COL3 (in xsl:template or xsl:for-each) ...
  <xsl:choose>
   <xsl:when test="@Type='Dimesion'">
    <xsl:variable name="pos" select="position()"/>
    <xsl:variable name="val" select="."/>
    <xsl:if test="not(../preceding-sibling::*[1]/*[$pos and .=$val])">
     <xsl:variable name="rowspan">
      <xsl:apply-templates select="." mode="calc-rowspan"/>
     </xsl:variable>
     <td rowspan="{$rowspan}">
      <xsl:value-of select="."/>
     </td>
    </xsl:if>
   </xsl:when>
   <xsl:otherwise>
    <td>
     <xsl:value-of select="."/>
    </td>
   </xsl:otherwise>
  </xsl:choose>
 ...

 <xsl:template match="*" mode="calc-rowspan">
  <xsl:param name="i" select="1"/>
  <xsl:variable name="pos" select="position()"/>
  <xsl:variable name="val" select="."/>
  <xsl:variable name="next" select="../following-sibling::*[1]/*[$pos and .=$val]"/>
  <xsl:choose>
   <xsl:when test="$next">
    <xsl:apply-templates select="$next" mode="calc-rowspan">
     <xsl:with-param name="i" select="$i + 1"/>
    </xsl:apply-templates>
   </xsl:when>
   <xsl:otherwise><xsl:value-of select="$i"/></xsl:otherwise>
  </xsl:choose>
 </xsl:template>

BTW. do you have control over your input files? If so, consider
using one element name for the columns, ie:

 <ROW>
  <COL Type="Dimesion">A</COL>
  <COL Type="Dimesion">E</COL>
  <COL>1</COL>
  <COL>2</COL>
 </ROW>

rather than

 <ROW>
  <COL0 Type="Dimesion">A</COL0>
  <COL1 Type="Dimesion">E</COL1>
  <COL2>1</COL2>
  <COL3>2</COL3>
 </ROW>

Then you can select and match "COL" instead of "*" 
(the column index is taken from its position, anyway)

HTH,
Anton Triest

Current Thread