<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
   <xsl:key name="getBall" match="ROOT/BALLS/BALL" use="@id" />

   <xsl:key name="getSelectedBall" match="ROOT/COLS/COL/BALL" use="@id" />

   <xsl:param name="rows">15</xsl:param>

   <xsl:param name="cols">10</xsl:param>

   <xsl:variable name="selcount" select="count(ROOT/COLS/COL/BALL)" />

   <xsl:param name="ball">70</xsl:param>

   <xsl:variable name="nball" select="//ROOT/BALLS/BALL[@id=$ball]" />

   <xsl:variable name="color" select="'white'" />

   <xsl:template match="/">
            <TABLE cellspacing="0" cellpadding="0" style="BACKGROUND-COLOR: {$color};">
               <xsl:call-template name="row-print" />
            </TABLE>
   </xsl:template>

   <xsl:template name="row-print">
      <xsl:param name="row" select="$rows - 1" />

      <xsl:if test="$row &gt;= 0">
         <TR>
            <xsl:call-template name="cell-print">
               <xsl:with-param name="row" select="$row" />
            </xsl:call-template>
         </TR>

         <xsl:call-template name="row-print">
            <xsl:with-param name="row" select="$row - 1" />
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

   <xsl:template name="cell-print">
      <xsl:param name="col" select="0" />

      <xsl:param name="row" />

      <xsl:variable name="cell" select="($row * $cols) + $col" />

      <xsl:variable name="thisBall" select="key('getBall',$cell)" />

      <xsl:variable name="selBall" select="key('getSelectedBall',$cell)" />

      <xsl:if test="$col &lt; $cols">
         <xsl:choose>
            <xsl:when test="not($thisBall)">
               <TD class="cell" style="cursor:default;BORDER:white #EEA500 1px;color:#EEA500;background-color:#EEA500">.</TD>
            </xsl:when>

            <xsl:when test="not($selBall) or not($selcount &gt; 1)">
               <TD class="cell" id="c{$cell}" style="color:{$color};cursor:hand;BORDER:white solid 1px;background-color:{$color}" onclick="select_ball({$cell})">
                  <IMG src="{$thisBall/@color}.gif" height="21" width="21" />
               </TD>
            </xsl:when>

            <xsl:otherwise>
               <xsl:variable name="style">color: 
               <xsl:value-of select="$thisBall/@color" />

               ;cursor:hand;background-color:
               <xsl:value-of select="$color" />

               ; 
               <xsl:choose>
                  <xsl:when test="$selBall/TOP">BORDER-TOP:black solid 1px;</xsl:when>

                  <xsl:otherwise>BORDER-TOP:
                  <xsl:value-of select="$color" />

                  solid 1px;</xsl:otherwise>
               </xsl:choose>

               <xsl:choose>
                  <xsl:when test="$selBall/BOTTOM">BORDER-BOTTOM:black solid 1px;</xsl:when>

                  <xsl:otherwise>BORDER-BOTTOM:
                  <xsl:value-of select="$color" />

                  solid 1px;</xsl:otherwise>
               </xsl:choose>

               <xsl:choose>
                  <xsl:when test="$selBall/LEFT">BORDER-LEFT:black solid 1px;</xsl:when>

                  <xsl:otherwise>BORDER-LEFT:
                  <xsl:value-of select="$color" />

                  solid 1px;</xsl:otherwise>
               </xsl:choose>

               <xsl:choose>
                  <xsl:when test="$selBall/RIGHT">BORDER-RIGHT:black solid 1px;</xsl:when>

                  <xsl:otherwise>BORDER-RIGHT:
                  <xsl:value-of select="$color" />

                  solid 1px;</xsl:otherwise>
               </xsl:choose>
               </xsl:variable>

               <TD class="cell" id="c{$cell}" style="{normalize-space($style)}" ondblclick="removearea({$cell})">
                  <IMG src="{$thisBall/@color}.gif" height="21" width="21" />
               </TD>
            </xsl:otherwise>
         </xsl:choose>

         <xsl:call-template name="cell-print">
            <xsl:with-param name="col" select="$col + 1" />

            <xsl:with-param name="row" select="$row" />
         </xsl:call-template>
      </xsl:if>
   </xsl:template>
</xsl:transform>


