<xsl:stylesheet xmlns:exsl="http://exslt.org/common" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="exsl" version="1.0">
    <xsl:variable name="neighbours">
        <n>0</n>
        <n>1</n>
        <n>2</n>
        <n>3</n>
        <n>4</n>
        <n>5</n>
        <n>6</n>
        <n>7</n>
        <n>8</n>
    </xsl:variable>
    <xsl:variable name="l2d" select="/Doc/automatum/@l2d"/>
    <xsl:variable name="d2l" select="/Doc/automatum/@d2l"/>
    <xsl:variable name="density" select="/Doc/automatum/@density"/>
    <xsl:template match="/Doc">
        <html>
            <body>
                <h4>
                    <a href="index.xml">Home</a>
                </h4>
                <h2>Cellular Automata generalizing Conway's Game of Life.</h2>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="automatum">
        <p>Click on image toggles zoom on that area.</p>
        <table>
            <tr>
                <td cellpadding="20">
                    <applet code="randomlifezoom" archive="randomlifezoom.jar" width="200" height="200">
                        <param name="density">
                            <xsl:attribute name="value">
                                <xsl:value-of select="$density"/>
                            </xsl:attribute>
                        </param>
                        <param name="live">
                            <xsl:attribute name="value">
                                <xsl:value-of select="$l2d"/>
                            </xsl:attribute>
                        </param>
                        <param name="dead">
                            <xsl:attribute name="value">
                                <xsl:value-of select="$d2l"/>
                            </xsl:attribute>
                        </param>
                    </applet>
                </td>
                <td>
                    <form>
                        <table border="1">
                            <tr>
                                <td>Density</td>
                                <td>
                                    <input type="text" name="density">
                                        <xsl:attribute name="value">
                                            <xsl:value-of select="$density"/>
                                        </xsl:attribute>
                                    </input>
                                </td>
                            </tr>
                            <tr>
                                <td>Live to Dead</td>
                                <td>
                                    <table>
                                        <tr>
                                            <td colspan="9">Neighbours</td>
                                        </tr>
                                        <tr>
                                            <xsl:for-each select="exsl:node-set($neighbours)/n">
                                                <td align="center">
                                                    <xsl:value-of select="."/>
                                                </td>
                                            </xsl:for-each>
                                        </tr>
                                        <tr>
                                            <xsl:for-each select="exsl:node-set($neighbours)/n">
                                                <td>
                                                    <input type="checkbox" name="{concat('ld_',.)}">
                                                        <xsl:if test="contains($l2d,.)">
                                                            <xsl:attribute name="checked" select="true"/>
                                                        </xsl:if>
                                                    </input>
                                                </td>
                                            </xsl:for-each>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td>Dead to Live</td>
                                <td>
                                    <table>
                                        <tr>
                                            <td colspan="9">Neighbours</td>
                                        </tr>
                                        <tr>
                                            <xsl:for-each select="exsl:node-set($neighbours)/n">
                                                <td align="center">
                                                    <xsl:value-of select="."/>
                                                </td>
                                            </xsl:for-each>
                                        </tr>
                                        <tr>
                                            <xsl:for-each select="exsl:node-set($neighbours)/n">
                                                <td>
                                                    <input type="checkbox" name="{concat('dl_',.)}">
                                                        <xsl:if test="contains($d2l,.)">
                                                            <xsl:attribute name="checked" select="true"/>
                                                        </xsl:if>
                                                    </input>
                                                </td>
                                            </xsl:for-each>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr>
                                <td>Description</td>
                                <td>
                                    <textarea name="description" cols="60" rows="4">
                                        <xsl:value-of select="description"/>
                                    </textarea>
                                </td>
                            </tr>
                            <tr>
                                <td/>
                                <td>
                                    <input type="submit" name="mode" value="run"/>
                                    <input type="submit" name="mode" value="life"/>
                                    <input type="submit" name="mode" value="random"/>
                                    <input type="submit" name="mode" value="save"/>
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
    </xsl:template>
    <xsl:template match="log">
        <h2>Log of finds</h2>
        <table border="1">
            <tr>
                <th/>
                <th>Live to Dead</th>
                <th>Dead to Live</th>
                <th>Density</th>
                <th>Description</th>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="record">
        <tr>
            <td>
                <a>
                    <xsl:attribute name="href">
                        <xsl:text>lifelog.xql?mode=run&amp;l2d=</xsl:text>
                        <xsl:value-of select="@l2d"/>
                        <xsl:text>&amp;d2l=</xsl:text>
                        <xsl:value-of select="@d2l"/>
                        <xsl:text>&amp;density=</xsl:text>
                        <xsl:value-of select="@density"/>
                    </xsl:attribute>
                Run
                </a>
            </td>
            <td>
                <xsl:value-of select="@l2d"/>
            </td>
            <td>
                <xsl:value-of select="@d2l"/>
            </td>
            <td>
                <xsl:value-of select="@density"/>
            </td>
            <td>
                <xsl:value-of select="description"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
