Re: [xsl] Complex HTML Table

Subject: Re: [xsl] Complex HTML Table
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 29 Jan 2001 21:10:13 +0000
Hi MK,

>I'm trying to implement a solution to format the below XML into the
>below HTML. Any suggestions on how I'd go about doing so would be
>greatly appreciated.

There's a difference between the HTML source that you've provided and
the ASCII-art picture of the table that you've given, but I'll assume
that you meant the ASCII-art picture.  I think what you're getting at
is that the SKU elements are paired: one in the first column, one in
the second, with the ROW1 and ROW2 elements giving the rows in each of
these columns.

You first need one table element per SKUCOLLECTION element:

<xsl:template match="SKUCOLLECTION">

Within this, you want to create one pair of rows per pair of SKU
elements. As usual with grouping by position, you need to identify the
*first* of each of these pairs of SKU elements - the odd ones - and
apply templates to them.  You can use the 'mod' operator to identify
the odd SKU elements - they're the ones whose position() mod 2 is
equal to 1:

<xsl:template match="SKUCOLLECTION">
      <xsl:apply-templates select="SKU[position() mod 2 = 1]" />

Now, whenever you have a template matching a SKU element, you know
it's one of these odd ones.  For each of the odd SKU elements, you
want to create four cells: the first column contains the ROW1 and ROW2
elements from this (odd) SKU element and the second the ROW1 and ROW2
elements from the next (even) SKU element.

You can get the next (even) SKU element using the following-sibling::

   <xsl:variable name="nextSKU" select="following-sibling::SKU[1]" />

Once you've got this, it's easy to get the values for the second
column.  The complete template looks like:

<xsl:template match="SKU">
   <xsl:variable name="nextSKU" select="following-sibling::SKU[1]" />
      <td><xsl:value-of select="ROW1" /></td>
      <td><xsl:value-of select="$nextSKU/ROW1" /></td>
      <td><xsl:value-of select="ROW2" /></td>
      <td><xsl:value-of select="$nextSKU/ROW2" /></td>

>Does XSL give you the ability to store HTML content within variables?
>This may be one way of approaching this problem.

You can store HTML content within variables in XSLT, by specifying the
HTML content within the content of the xsl:variable instruction:

  <xsl:variable name="nextcell">
    <tr><xsl:value-of select="ROW1" /></tr>

However, I'm not sure how this would help in your problem: in
particular, you can't reassign values to variables in XSLT, so you
couldn't do something like assign a row to a global variable while
processing one SKU element, then use it when processing the next,
which is the only thing I can think of that you might have thought
might help.
Anyway, I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread