xsl-list
[Top] [All Lists]

Re: table in html with exactly four columns in xsl

2003-09-26 01:53:34
Hi Markus,

I am a beginner with xml, and my problem is to create a table in
html with rows and exactly 4 columns to number automatic a few
forms. The last row should be filled up to 4 columns with empty
cells or one cell with "colspan". this last row problem i could not
solved!

The template that's generating the rows is:

<xsl:template match="form" mode="row">
<tr>
  <xsl:apply-templates select=". |following-sibling::form[position() &lt;
$cols]" mode="cell" />
</tr>
</xsl:template>

What you have to do here is test to see how many cells are being
filled. If it's less than four, then you need to create extra cells
(it's easiest to create one cell that spans the appropriate number of
cells).

So, collect the <form> elements that are used in the row into a
variable, and apply templates to the <form> elements in that variable:

<xsl:template match="form" mode="row">
  <tr>
    <xsl:variable name="cells"
      select=". | following-sibling::form[position() &lt; $cols]" />
    <xsl:apply-templates select="$cells" mode="cell" />
    ...
  </tr>
</xsl:template>

You can calculate how many extra cells you need by subtracting the
number of cells (which you can get using the count() function) from
the number of cols. Hold this in a variable:

<xsl:template match="form" mode="row">
  <tr>
    <xsl:variable name="cells"
      select=". | following-sibling::form[position() &lt; $cols]" />
    <xsl:apply-templates select="$cells" mode="cell" />
    <xsl:variable name="extra" select="$cols - count($cells)" />
    ...
  </tr>
</xsl:template>

Then test whether you need any extra -- if $extra is more than 0:

<xsl:template match="form" mode="row">
  <tr>
    <xsl:variable name="cells"
      select=". | following-sibling::form[position() &lt; $cols]" />
    <xsl:apply-templates select="$cells" mode="cell" />
    <xsl:variable name="extra" select="$cols - count($cells)" />
    <xsl:if test="$extra > 0">
      ...
    </xsl:if>
  </tr>
</xsl:template>

If it is, the extra cell needs to have a colspan attribute whose value
is $extra; you can set it with an attribute value template:

<xsl:template match="form" mode="row">
  <tr>
    <xsl:variable name="cells"
      select=". | following-sibling::form[position() &lt; $cols]" />
    <xsl:apply-templates select="$cells" mode="cell" />
    <xsl:variable name="extra" select="$cols - count($cells)" />
    <xsl:if test="$extra > 0">
      <td colspan="{$extra}" />
    </xsl:if>
  </tr>
</xsl:template>

Of course, this calculation is only applicable to the last of the
rows, so you could add an extra <xsl:if> to test whether you're on the
last of the rows -- whether the position() of the current <form>
element in the set of <form> elements that's being processed is
last():

<xsl:template match="form" mode="row">
  <tr>
    <xsl:variable name="cells"
      select=". | following-sibling::form[position() &lt; $cols]" />
    <xsl:apply-templates select="$cells" mode="cell" />
    <xsl:if test="position() = last()">
      <xsl:variable name="extra" select="$cols - count($cells)" />
      <xsl:if test="$extra > 0">
        <td colspan="{$extra}" />
      </xsl:if>
    </xsl:if>
  </tr>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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