Re: [xsl] Formatting table information

Subject: Re: [xsl] Formatting table information
From: Kamal Bhatt <kbhatt@xxxxxxxxx>
Date: Tue, 06 Dec 2005 09:48:06 +1100
Here is my code for the generation of tables:

<xsl:template match="Table">
<xsl:param name="title"/>
<xsl:variable name="table-pos" select="position()"/>
<xsl:text>&newln;</xsl:text>
<xsl:variable name="length">
<xsl:call-template name="longest-str">
<xsl:with-param name="set" select="Row/Cell[1]"/>
</xsl:call-template>
</xsl:variable>


<xsl:for-each select="Row">
<xsl:variable name="padded-cell">
<xsl:call-template name="append-pad">
<xsl:with-param name="pad-var" select="normalize-space(Cell[1])"/>
<xsl:with-param name="length" select="$length"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="position() = 1 and $table-pos = 1"> <!-- TEXT OUTPUT BEGINS -->
<xsl:value-of select="$title"/>:<xsl:text>&tab;</xsl:text>
<xsl:value-of select="$padded-cell"/>
<xsl:text>&tab;</xsl:text>
<xsl:value-of select="normalize-space(Cell[2])"/>
<xsl:if test="position() != last()">
<xsl:text>&newln;</xsl:text>
</xsl:if>
<!-- TEXT OUTPUT ENDS -->
</xsl:when>
<xsl:otherwise>
<!-- TEXT OUTPUT BEGINS -->
<xsl:text>&tab;</xsl:text>
<xsl:value-of select="$padded-cell"/>
<xsl:text>&tab;</xsl:text>
<xsl:value-of select="normalize-space(Cell[2])"/>
<xsl:if test="position() != last()">
<xsl:text>&newln;</xsl:text>
</xsl:if>
<!-- TEXT OUTPUT ENDS -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>


<xsl:template name="longest-str">
  <xsl:param name="set"/>
  <xsl:param name="curr-max-size" select="0"/>
  <xsl:param name="index" select="1"/>

<xsl:variable name="curr-size" select="string-length($set[$index])"/>

<xsl:choose>
<xsl:when test="$index > count($set)">
<xsl:value-of select="$curr-max-size"/>
</xsl:when>
<xsl:when test="string-length($set[$index]) > $curr-max-size">
<xsl:call-template name="longest-str">
<xsl:with-param name="set" select="$set"/>
<xsl:with-param name="index" select="$index + 1"/>
<xsl:with-param name="curr-max-size" select="$curr-size"/>
</xsl:call-template> </xsl:when>


<xsl:otherwise>
<xsl:call-template name="longest-str">
<xsl:with-param name="set" select="$set"/>
<xsl:with-param name="index" select="$index + 1"/>
<xsl:with-param name="curr-max-size" select="$curr-max-size"/>
</xsl:call-template> </xsl:otherwise> </xsl:choose>
</xsl:template>


<!-- Code taken from www.dpawson.co.uk/xsl/sect2/padding.html -->

<xsl:template name="append-pad">
   <xsl:param name="pad-char"><xsl:text>&space;</xsl:text></xsl:param>
   <xsl:param name="pad-var"/>
   <xsl:param name="length"/>
   <xsl:choose>
   <xsl:when test="string-length($pad-var) &lt; $length">
       <xsl:call-template name="append-pad">
        <xsl:with-param name="pad-char" select="$pad-char"/>
        <xsl:with-param name="pad-var"
                select="concat($pad-var, $pad-char)"/>
        <xsl:with-param name="length" select="$length"/>
       </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
       <xsl:value-of select="substring($pad-var, 1, $length)"/>
   </xsl:otherwise>
   </xsl:choose>
</xsl:template>

Ragulf Pickaxe wrote:

In a previous post I mentioned that that I have created a template to
format lines to a fixed width and a function to format a table. When I
said table, I meant something with only two columns. Now, I need to
format a table with three columns.



Hi Kamal,


I am not sure that I understand all your requirements, but for getting
3 columns, the solution is the same as for 2 columns, you will just
have to use modulo 3 instead of modulo 2. I do not know whether this
is enough to get you in the right direction.

Regards,
Ragulf Pickaxe :-)

Current Thread