Re: [xsl] creating a table with xslt

Subject: Re: [xsl] creating a table with xslt
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 17 Mar 2008 11:46:35 -0400
Kimberly,

You can certainly make multiple rows. And it doesn't matter whether you know in advance how many questions there are (500 or whatever). You only need to decide whether (a) you want to go across first and then down (across first is easier), (b) whether you want a fixed number of cells in each row, or a dynamic number determined somehow at runtime, and (c) how you want to fill empty spots in your table.

I'm going to assume you want to go across first, and that you want exactly 20 cells in each row, and that you're simply going to leave a stretch of empty space if your questions don't divide evenly by 20. This is the easiest case.

<xsl:for-each select="testresults/test[position() mod 20 = 1]">
  <!-- select every 20th 'test' element, that is, #s 1, 21, 41, etc. -->
  <tr>
    <xsl:for-each select=".|following-sibling[position() &lt; 20]">
    <!-- now select the current 'test' element plus its 19 nearest
       following siblings -->
    ...
      <td> <!-- making a cell for each of these -->
       ...
      </td>
    </xsl:for-each>
  </tr>
</xsl:for-each>

etc. Pretty simple, really.

A couple of other things. The repetition of the code inside the table cell suggests that you should move your choose options deeper, as in

<TD>
  <a href="{plot_path}" target="_blank">
    <xsl:choose>
      <xsl:when test="result &gt; 0">
        <IMG SRC="red.jpg"/>
      <xsl:when>
      <xsl:when test="result = 0">
        <IMG SRC="green.jpg"/>
      </xsl:when>
... etc.

Indeed, you could use templates instead of a choose/when/otherwise construction. For example, apply templates to the result elements and match them with different templates depending on their values:

<xsl:template match="result[. &gt; 0]">
  <IMG SRC="red.jpg"/>
<xsl:template>

<xsl:template match="result[. = 0]">
  <IMG SRC="green.jpg"/>
<xsl:template>

then in the main template:

...
<TD>
  <a href="{plot_path}" target="_blank">
    <xsl:apply-templates select="result"/>
  </a>
</TD>
...

This would be very XSLT-like.

Cheers,
Wendell

At 12:02 AM 3/17/2008, you wrote:
Hi,
This is my first post on  your forum - I was searching around for how
to implement a counter in xslt and this brought me to your user group.
I'm looking to do make a very simple table from an xml file that is
output from a servlet.
 There is no text in the table.
Imagine a test that has 500 questions and each cell in the table
represents a question - they are red/green/grey square jpeg's depeding
on if you got the question correct/incorrect or n/a (you didn't answer
it)
 Ideally you'd end up with alot of green squares and a few red squares
- then you could click on any square and the page would update with a
box at the bottom showing you all the details of that question.
I have pasted some simple xslt code below (it doesn't include all the
elements or features - it is just for testing purposes)  that goes
through each element in the xml file and makes a row for it in the
table. I'm having problems figuring out how to automatically make
multiple rows and columns with data like this - obviously a simple
foreach loop won't work. All the data is the same - it is just in a
table to structure it for ease of viewing so columns and rows don't
actually map to anything - it's all the same. The other problem is
that a user may not answer all the questions and can only answer some
of them - meaning there is not guarranteed to be 500 elements in the
xml file.
 Maybe a table isn't the best structure to use in this case?
Any advice?


<xsl:template match="/">


<HTML>

<BODY>

<TABLE BORDER="0" CELLSPACING="0" WIDTH="80%">

<xsl:for-each select="testresults/test">

    <TR>
       <xsl:choose>
        <xsl:when test="result &gt; 0">
        <TD>
        <a href="{plot_path}" target="_blank">
         <IMG SRC="red.jpg"/>
        </a>
        </TD>
        </xsl:when>

        <xsl:when test="result = 0">
        <TD>
        <a href="{plot_path}" target="_blank">
         <IMG SRC="green.jpg"/>
        </a>
        </TD>
        </xsl:when>
        <xsl:otherwise>
        <TD>
        <a href="{plot_path}" target="_blank">
         <IMG SRC="grey.jpg"/>
        </a>
        </TD>
        </xsl:otherwise>
       </xsl:choose>
    </TR>
    </xsl:for-each>
    </TABLE>
     </BODY>
    </HTML>
    </xsl:template>
</xsl:stylesheet>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread