This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: acessing attribute values and adding them to a param, looping


Hi Sebastian,

I think that you would be better off using the td elements to
determine what the empty cells should look like rather than using
counts to iterate - usually in XSLT it's a lot easier to use the
structure of the source tree by applying templates to nodes rather
than querying into it using indexes.

So, I would approach this problem by having a template that matches
the tr element and produces a tr element in return.  The initial cells
within the row should be generated according to the td elements that
you have in the source XML - apply templates to get them:

<xsl:template match="tr">
   <xsl:comment>Spacer row generated</xsl:comment>
   <tr>
      <xsl:apply-templates select="td" />
      <td bgcolor="#4E4D49" width="1" height="1">
         <img src="img/trans.gif" width="1" height="1" border="0" />
      </td>
      <td width="10" height="1" background="img/schatten_re.gif">
         <img src="img/trans.gif" width="10" height="1" border="0" />
      </td>
   </tr>
</xsl:template>

Now, for each of the td elements, you want to create four or more td
elements - how many you want to create depends on the colspan
attribute. That means you need a parameter holding the number of
copies to be generated (usually 1) so that you can apply the same
template multiple times, counting down how many should be made step by
step:

<xsl:template match="td">
   <xsl:param name="repeat">
      <xsl:choose>
         <xsl:when test="@colspan">
            <xsl:value-of select="@colspan" />
         </xsl:when>
         <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
   </xsl:param>
   ...
   <!-- create table cells -->
   ...
   <xsl:if test="$repeat > 1">
      <xsl:apply-templates select=".">
         <xsl:with-param name="repeat" select="$repeat - 1" />
      </xsl:apply-templates>
   </xsl:if>
</xsl:template>

As far as creating the table cells goes - you can test the content of
the td element that you're currently working with to see whether it
has the relevant content, and thus work out whether to add a colour to
it:

<xsl:template match="td">
   <xsl:param name="repeat">
      <xsl:choose>
         <xsl:when test="@colspan">
            <xsl:value-of select="@colspan" />
         </xsl:when>
         <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
   </xsl:param>
   <td width="1" height="1">
      <xsl:if test="design/@key = 'vruler'">
         <xsl:attribute name="bgcolor">#4E4D49</xsl:attribute>
      </xsl:if>
      <img src="img/trans.gif" width="1" height="1" border="0" />
   </td>
   <td width="5" height="1">
      <img src="img/trans.gif" width="5" height="1" border="0"/>
   </td>
   <td width="1" height="1">
      <img src="img/trans.gif" width="1" height="1" border="0"/>
   </td>
   <td width="5" height="1">
      <img src="img/trans.gif" width="5" height="1" border="0"/>
   </td>
   <xsl:if test="$repeat > 1">
      <xsl:apply-templates select=".">
         <xsl:with-param name="repeat" select="$repeat - 1" />
      </xsl:apply-templates>
   </xsl:if>
</xsl:template>

The disadvantage of this method is that it makes it harder to output
spacer cells for the tr elements in your source that have less than
the maximum number of cells in them. If that's a problem, I'd address
it by adding a looper template like the one you have, which can run a
number of times (and will in these cases be totally independent of the
content of the table, since by definition there aren't td elements
that are the equivalent of the cells that it's creating).  Then I'd
have a summing template that can take a row and return the number of
columns that it defines (taking into account the @colspan attributes
on the td elements that it contains - you probably have this already
in order to work out the maximum number of columns in a row).  Use
this, with the maximum number ($max-col) to work out how many columns
are left to do, and pass that number into the looper template to
create them.

I hope that helps,

Jeni

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



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]