<?xml version="1.0" encoding="utf-8"?>

<!-- =============================================================== -->
<!--                                                                 -->
<!-- This stylesheet makes part of RenderX XSLFO Test Suite.         -->
<!--                                                                 -->
<!-- It produces a sequence of chess diagram in XSL FO, starting     -->
<!-- from a PGN-like XML notation. The result can be further         -->
<!-- converted to a page-oriented format (PDF or PostScript).        -->
<!--                                                                 -->
<!-- XSL FO version taken into account:                              -->
<!--     http://www.w3.org/TR/2000/CR-xsl-20001121                   -->
<!--                                                                 -->
<!--     Author: Anton Dovgyallo                                     -->
<!--                                                                 -->
<!-- (c) RenderX, 1999-2000. Permission to copy and modify is        -->
<!-- granted, provided that any derived work contain a reference     -->
<!-- to this original document.                                      -->
<!-- =============================================================== -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
	<xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>

	<!-- ****************************************************** -->
	<!-- * Root template. Sets page layout.                   * -->
	<!-- ****************************************************** -->

	<xsl:template match="chessgame">
		<div class="chess">
			<h2><xsl:value-of select="@site"/><xsl:text>, </xsl:text><xsl:value-of select="@date"/></h2>

			<p><xsl:value-of select="@event"/>, round <xsl:value-of select="@round"/>.</p>

			<dl class="chess-info">
				<dt>White</dt>
				<dd><xsl:value-of select="@white"/>.</dd>

				<dt>Black</dt>
				<dd><xsl:value-of select="@black"/>.</dd>

				<xsl:if test="@opening != ''">
					<dt>Opening</dt>
					<dd><xsl:value-of select="@opening"/>.</dd>
				</xsl:if>

				<dt>Result</dt>
				<dd><xsl:apply-templates select="@result"/></dd>
			</dl>

			<xsl:apply-templates select="move[1]"/>
		</div>
	</xsl:template>

	<!-- Processing result attribute. The first template handles   -->
	<!-- a case where result is specified in an unknown form;      -->
	<!-- three subsequent templates handle standard result values. -->

	<xsl:template match="@result">
		Unknown.
	</xsl:template>

	<xsl:template match="@result[.='1-0']">
		White wins.
	</xsl:template>

	<xsl:template match="@result[.='0-1']">
		Black wins.
	</xsl:template>

	<xsl:template match="@result[.='1/2-1/2']">
		Draw.
	</xsl:template>

	<!-- ****************************************************** -->
	<!-- * Applies to a single move (i.e. two half-moves).    * -->
	<!-- * Recursive: at the end, applies itself to the next  * -->
	<!-- * move in the chessgame.                             * -->
	<!-- ****************************************************** -->

	<xsl:template match="move">
		<xsl:param name="chessboard" select="'RNBQKBNRPPPPPPPPxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpppppppprnbqkbnr'"/>

		<!-- $move-white contains the chessboard after the white's move -->
		<xsl:variable name="move-white">
			<xsl:call-template name="move-piece">
				<xsl:with-param name="chessboard" select="$chessboard"/>
				<xsl:with-param name="move" select="white"/>
				<xsl:with-param name="player" select="'white'"/>
			</xsl:call-template>
		</xsl:variable>

		<!-- $move-black contains the chessboard after the black's move -->
		<xsl:variable name="move-black">
			<!-- The test serves to handle incomplete moves at the end, -->
			<!-- when the game stops at the white's half-move.          -->
			<xsl:choose>
				<xsl:when test="black">
					<xsl:call-template name="move-piece">
						<xsl:with-param name="chessboard" select="$move-white"/>
						<xsl:with-param name="move" select="black"/>
						<xsl:with-param name="player" select="'black'"/>
					</xsl:call-template>
				</xsl:when>

				<xsl:otherwise>
					<xsl:value-of select="''"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- Draw a move diagram -->
		<xsl:call-template name="render-move">
			<xsl:with-param name="move-white" select="$move-white"/>
			<xsl:with-param name="move-black" select="$move-black"/>
		</xsl:call-template>

		<!-- Recurse -->
		<xsl:apply-templates select="following-sibling::move[1]">
			<xsl:with-param name="chessboard" select="$move-black"/>
		</xsl:apply-templates>
	</xsl:template>


	<!-- ****************************************************** -->
	<!--  Moves the piece from one position to another.         -->
	<!--  Special cases (O-O and O-O-O) are treated.            -->
	<!--  Invoked by the 'move' template for each half-move.    -->
	<!--  Parameters:                                           -->
	<!--    $chessboard holds the initial chessboard state;     -->
	<!--    $move is a PGN-like description of the move;        -->
	<!--    $player = 'white'|'black' - needed for castlings.   -->
	<!-- ****************************************************** -->

	<xsl:template name="move-piece">
		<xsl:param name="chessboard"/>
		<xsl:param name="move"/>
		<xsl:param name="player"/>

		<xsl:choose>
			<!-- O-O castles short (King's side) -->
			<xsl:when test="$move = 'O-O'">
				<xsl:variable name="y">
					<xsl:choose>
						<xsl:when test="$player = 'white'">1</xsl:when>
						<xsl:otherwise>8</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>

				<xsl:variable name="result">
					<xsl:call-template name="move-piece">
						<xsl:with-param name="chessboard" select="$chessboard"/>
						<xsl:with-param name="player" select="$player"/>
						<xsl:with-param name="move" select="concat('K ', 'e', $y, '-', 'g', $y)"/>
					</xsl:call-template>
				</xsl:variable>

				<xsl:call-template name="move-piece">
					<xsl:with-param name="chessboard" select="$result"/>
					<xsl:with-param name="player" select="$player"/>
					<xsl:with-param name="move" select="concat('R ', 'h', $y, '-', 'f', $y)"/>
				</xsl:call-template>
			</xsl:when>

			<!-- O-O-O castles long (Queen's side) -->

			<xsl:when test="$move = 'O-O-O'">
				<xsl:variable name="y">
					<xsl:choose>
						<xsl:when test="$player = 'white'">1</xsl:when>
						<xsl:otherwise>8</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>

				<xsl:variable name="result">
					<xsl:call-template name="move-piece">
						<xsl:with-param name="chessboard" select="$chessboard"/>
						<xsl:with-param name="player" select="$player"/>
						<xsl:with-param name="move" select="concat('K ', 'e', $y, '-', 'c', $y)"/>
					</xsl:call-template>
				</xsl:variable>

				<xsl:call-template name="move-piece">
					<xsl:with-param name="chessboard" select="$result"/>
					<xsl:with-param name="player" select="$player"/>
					<xsl:with-param name="move" select="concat('R ', 'a', $y, '-', 'd', $y)"/>
				</xsl:call-template>
			</xsl:when>

			<!-- parse move -->

			<xsl:otherwise>
				<xsl:variable name="parsed-move">
					<xsl:call-template name="parse-move">
						<xsl:with-param name="chessboard" select="$chessboard"/>
						<xsl:with-param name="move" select="$move"/>
						<xsl:with-param name="player" select="$player"/>
					</xsl:call-template>
				</xsl:variable>

				<xsl:variable name="piece">
					<xsl:choose>
						<xsl:when test="$player = 'white'">
							<xsl:value-of select="substring-before($parsed-move, ' ')"/>
						</xsl:when>

						<xsl:otherwise>
							<xsl:value-of select="translate(substring-before($parsed-move, ' '), 'RNBQKP', 'rnbqkp')"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>

				<xsl:variable name="coords" select="substring(substring-after($parsed-move, ' '), 1, 5)"/>

				<xsl:variable name="from" select="(substring(substring-before($coords, '-'), 2, 1) - 1) * 8 + string-length(substring-before('abcdefgh', substring(substring-before($coords, '-'), 1, 1))) + 1"/>

				<xsl:variable name="to" select="(substring(substring-after ($coords, '-'), 2, 1) - 1) * 8 + string-length(substring-before('abcdefgh', substring(substring-after ($coords, '-'), 1, 1))) + 1"/>

				<xsl:variable name="get-piece" select="concat(substring($chessboard, 1, $from - 1), 'x', substring($chessboard, $from + 1))"/>

				<xsl:value-of select="concat(substring($get-piece, 1, $to - 1), $piece, substring($get-piece, $to + 1))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- SAN handling. -->
	<xsl:template name="parse-move">
		<xsl:param name="chessboard"/>
		<xsl:param name="move"/>
		<xsl:param name="player"/>

		<xsl:variable name="piece-before">
			<xsl:choose>
				<!-- It's a pawn. -->
				<xsl:when test="string-length($move) = 2">P</xsl:when>

				<xsl:otherwise><xsl:value-of select="substring($move, 1, 1)"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="piece">
			<xsl:choose>
				<xsl:when test="$player = 'white'">
					<xsl:value-of select="$piece-before"/>
				</xsl:when>

				<xsl:otherwise>
					<xsl:value-of select="translate($piece-before, 'RNBQKP', 'rnbqkp')"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="capture">
			<xsl:if test="contains($move, 'x')"><xsl:value-of select="' x'"/></xsl:if>
		</xsl:variable>

		<xsl:variable name="move-stripped">
			<xsl:value-of select="translate(translate(translate($move, 'x', ''), '+', ''), $piece, '')"/>
		</xsl:variable>

		<xsl:variable name="finish">
			<xsl:choose>
				<xsl:when test="string-length($move-stripped) = 2"><xsl:value-of select="$move-stripped"/></xsl:when>

				<xsl:when test="string-length($move-stripped) = 3"><xsl:value-of select="substring($move-stripped, 2, 2)"/></xsl:when>

				<xsl:when test="string-length($move-stripped) = 4"><xsl:value-of select="substring($move-stripped, 3, 2)"/></xsl:when>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="start-file">
			<xsl:if test="starts-with($move-stripped, 'a') or starts-with($move-stripped, 'b') or starts-with($move-stripped, 'c') or starts-with($move-stripped, 'd') or starts-with($move-stripped, 'e') or starts-with($move-stripped, 'f') or starts-with($move-stripped, 'g') or starts-with($move-stripped, 'h')">
				<xsl:value-of select="substring($move-stripped, 1, 1)"/>
			</xsl:if>
		</xsl:variable>

		<xsl:variable name="start-rank">
			<xsl:if test="starts-with($move-stripped, '1') or starts-with($move-stripped, '2') or starts-with($move-stripped, '3') or starts-with($move-stripped, '4') or starts-with($move-stripped, '5') or starts-with($move-stripped, '6') or starts-with($move-stripped, '7') or starts-with($move-stripped, '8')">
				<xsl:value-of select="substring($move-stripped, 1, 1)"/>
			</xsl:if>
		</xsl:variable>

		<xsl:variable name="start">
			<xsl:call-template name="where-do-you-come-from">
				<xsl:with-param name="chessboard" select="$chessboard"/>
				<xsl:with-param name="finish" select="$finish"/>
				<xsl:with-param name="piece" select="$piece"/>
				<xsl:with-param name="piece-before" select="$piece-before"/>
				<xsl:with-param name="file" select="$start-file"/>
				<xsl:with-param name="rank" select="$start-rank"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:value-of select="concat($piece, ' ', $start, '-', $finish, $capture)"/>
	</xsl:template>

	<xsl:template name="where-do-you-come-from">
		<xsl:param name="chessboard" select="''"/>
		<xsl:param name="finish" select="''"/>
		<xsl:param name="piece" select="''"/>
		<xsl:param name="piece-before" select="''"/>
		<xsl:param name="file" select="''"/>
		<xsl:param name="rank" select="''"/>

		<xsl:choose>
			<xsl:when test="string-length($file) = 1">
				<xsl:variable name="my-file" select="string-length(substring-before('abcdefgh', $rank))"/>

				<xsl:variable name="my-rank">
					<xsl:call-template name="substring-position">
						<xsl:with-param name="string" select="substring($chessboard, number($my-file) * 8 - 7, 8)"/>
						<xsl:with-param name="substring" select="$piece"/>
					</xsl:call-template>
				</xsl:variable>

				<xsl:value-of select="concat($my-file, $my-rank)"/>
			</xsl:when>

			<xsl:when test="string-length($file) = 1">
				<xsl:variable name="my-rank">
					<xsl:call-template name="substring-position">
						<!-- Take every eighth character, starting from $file. -->

						<xsl:with-param name="string" select="concat(substring($chessboard, number($file), 1), substring($chessboard, number($file) + 8, 1), substring($chessboard, number($file) + 16, 1), substring($chessboard, number($file) + 24, 1), substring($chessboard, number($file) + 32, 1), substring($chessboard, number($file) + 40, 1), substring($chessboard, number($file) + 48, 1), substring($chessboard, number($file) + 56, 1))"/>
						<xsl:with-param name="substring" select="$piece"/>
					</xsl:call-template>
				</xsl:variable>

				<xsl:value-of select="concat($file, $my-rank)"/>
			</xsl:when>

			<xsl:otherwise>
				<xsl:variable name="my-finish" select="number((string-length(substring-before('abcdefgh', substring($finish, 1, 1)) * 8) + number(substring($finish, 2, 1)) - 8))"/>

				<xsl:variable name="coordinates">
					<xsl:choose>
						<xsl:when test="$piece-before = 'K'">
							<!-- Left and right (optional diagonals). -->
							<xsl:if test="($my-finish mod 8) &#60; 8">
								<xsl:value-of select="concat(string($my-finish + 1), ':')"/>

								<xsl:if test="($my-finish + 7) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 7), ':')"/>
								</xsl:if>

								<xsl:if test="($my-finish - 9) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 9), ':')"/>
								</xsl:if>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 1">
								<xsl:value-of select="concat(string($my-finish - 1), ':')"/>

								<xsl:if test="($my-finish + 9) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 9), ':')"/>
								</xsl:if>

								<xsl:if test="($my-finish - 7) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 7), ':')"/>
								</xsl:if>
							</xsl:if>

							<!-- Forwards and backwards. -->
							<xsl:if test="($my-finish + 8) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 8), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 8) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 8), ':')"/>
							</xsl:if>
						</xsl:when>

						<xsl:when test="$piece-before = 'Q'">
							<xsl:if test="($my-finish mod 8) &#60; 8">
								<xsl:value-of select="concat(string($my-finish + 1), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 7">
								<xsl:value-of select="concat(string($my-finish + 2), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 6">
								<xsl:value-of select="concat(string($my-finish + 3), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 5">
								<xsl:value-of select="concat(string($my-finish + 4), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 4">
								<xsl:value-of select="concat(string($my-finish + 5), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 3">
								<xsl:value-of select="concat(string($my-finish + 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 2">
								<xsl:value-of select="concat(string($my-finish + 7), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 1">
								<xsl:value-of select="concat(string($my-finish - 1), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 2">
								<xsl:value-of select="concat(string($my-finish - 2), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 3">
								<xsl:value-of select="concat(string($my-finish - 3), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 4">
								<xsl:value-of select="concat(string($my-finish - 4), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 5">
								<xsl:value-of select="concat(string($my-finish - 5), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 6">
								<xsl:value-of select="concat(string($my-finish - 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 7">
								<xsl:value-of select="concat(string($my-finish - 7), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 8) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 8), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 16) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 16), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 24) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 24), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 32) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 32), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 40) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 40), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 48) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 48), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 56) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 56), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 8) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 8), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 16) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 16), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 24) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 24), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 32) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 32), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 40) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 40), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 48) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 48), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 56) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 56), ':')"/>
							</xsl:if>

							<xsl:if test="(($my-finish + 7) mod 8) &#62; 0 and ($my-finish + 7) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 7), ':')"/>

								<xsl:if test="(($my-finish + 14) mod 8) &#62; 0 and ($my-finish + 14) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 14), ':')"/>

									<xsl:if test="(($my-finish + 21) mod 8) &#62; 0 and ($my-finish + 21) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 21), ':')"/>

										<xsl:if test="(($my-finish + 28) mod 8) &#62; 0 and ($my-finish + 28) &#60; 65">
											<xsl:value-of select="concat(string($my-finish + 28), ':')"/>

											<xsl:if test="(($my-finish + 35) mod 8) &#62; 0 and ($my-finish + 35) &#60; 65">
												<xsl:value-of select="concat(string($my-finish + 35), ':')"/>

												<xsl:if test="(($my-finish + 42) mod 8) &#62; 0 and ($my-finish + 42) &#60; 65">
													<xsl:value-of select="concat(string($my-finish + 42), ':')"/>

													<xsl:if test="(($my-finish + 49) mod 8) &#62; 0 and ($my-finish + 49) &#60; 65">
														<xsl:value-of select="concat(string($my-finish + 49), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish + 9) mod 8) &#62; 0 and ($my-finish + 9) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 9), ':')"/>

								<xsl:if test="(($my-finish + 18) mod 8) &#62; 0 and ($my-finish + 18) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 18), ':')"/>

									<xsl:if test="(($my-finish + 27) mod 8) &#62; 0 and ($my-finish + 27) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 27), ':')"/>

										<xsl:if test="(($my-finish + 36) mod 8) &#62; 0 and ($my-finish + 36) &#60; 65">
											<xsl:value-of select="concat(string($my-finish + 36), ':')"/>

											<xsl:if test="(($my-finish + 45) mod 8) &#62; 0 and ($my-finish + 45) &#60; 65">
												<xsl:value-of select="concat(string($my-finish + 45), ':')"/>

												<xsl:if test="(($my-finish + 54) mod 8) &#62; 0 and ($my-finish + 54) &#60; 65">
													<xsl:value-of select="concat(string($my-finish + 54), ':')"/>

													<xsl:if test="(($my-finish + 63) mod 8) &#62; 0 and ($my-finish + 63) &#60; 65">
														<xsl:value-of select="concat(string($my-finish + 63), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish - 7) mod 8) &#62; 0 and ($my-finish - 7) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 7), ':')"/>

								<xsl:if test="(($my-finish - 14) mod 8) &#62; 0 and ($my-finish - 14) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 14), ':')"/>

									<xsl:if test="(($my-finish - 21) mod 8) &#62; 0 and ($my-finish - 21) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 21), ':')"/>

										<xsl:if test="(($my-finish - 28) mod 8) &#62; 0 and ($my-finish - 28) &#62; 0">
											<xsl:value-of select="concat(string($my-finish - 28), ':')"/>

											<xsl:if test="(($my-finish - 35) mod 8) &#62; 0 and ($my-finish - 35) &#62; 0">
												<xsl:value-of select="concat(string($my-finish - 35), ':')"/>

												<xsl:if test="(($my-finish - 42) mod 8) &#62; 0 and ($my-finish - 42) &#62; 0">
													<xsl:value-of select="concat(string($my-finish - 42), ':')"/>

													<xsl:if test="(($my-finish - 49) mod 8) &#62; 0 and ($my-finish - 49) &#62; 0">
														<xsl:value-of select="concat(string($my-finish - 49), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish - 9) mod 8) &#62; 0 and ($my-finish - 9) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 9), ':')"/>

								<xsl:if test="(($my-finish - 18) mod 8) &#62; 0 and ($my-finish - 18) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 18), ':')"/>

									<xsl:if test="(($my-finish - 27) mod 8) &#62; 0 and ($my-finish - 27) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 27), ':')"/>

										<xsl:if test="(($my-finish - 36) mod 8) &#62; 0 and ($my-finish - 36) &#62; 0">
											<xsl:value-of select="concat(string($my-finish - 36), ':')"/>

											<xsl:if test="(($my-finish - 45) mod 8) &#62; 0 and ($my-finish - 45) &#62; 0">
												<xsl:value-of select="concat(string($my-finish - 45), ':')"/>

												<xsl:if test="(($my-finish - 54) mod 8) &#62; 0 and ($my-finish - 54) &#62; 0">
													<xsl:value-of select="concat(string($my-finish - 54), ':')"/>

													<xsl:if test="(($my-finish - 63) mod 8) &#62; 0 and ($my-finish - 63) &#62; 0">
														<xsl:value-of select="concat(string($my-finish - 63), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>
						</xsl:when>

						<xsl:when test="$piece-before = 'R'">
							<!-- Left and right. -->
							<xsl:if test="($my-finish mod 8) &#60; 8">
								<xsl:value-of select="concat(string($my-finish + 1), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 7">
								<xsl:value-of select="concat(string($my-finish + 2), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 6">
								<xsl:value-of select="concat(string($my-finish + 3), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 5">
								<xsl:value-of select="concat(string($my-finish + 4), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 4">
								<xsl:value-of select="concat(string($my-finish + 5), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 3">
								<xsl:value-of select="concat(string($my-finish + 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#60; 2">
								<xsl:value-of select="concat(string($my-finish + 7), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 1">
								<xsl:value-of select="concat(string($my-finish - 1), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 2">
								<xsl:value-of select="concat(string($my-finish - 2), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 3">
								<xsl:value-of select="concat(string($my-finish - 3), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 4">
								<xsl:value-of select="concat(string($my-finish - 4), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 5">
								<xsl:value-of select="concat(string($my-finish - 5), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 6">
								<xsl:value-of select="concat(string($my-finish - 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish mod 8) &#62; 7">
								<xsl:value-of select="concat(string($my-finish - 7), ':')"/>
							</xsl:if>

							<!-- Forwards and backwards. -->
							<xsl:if test="($my-finish + 8) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 8), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 16) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 16), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 24) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 24), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 32) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 32), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 40) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 40), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 48) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 48), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 56) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 56), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 8) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 8), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 16) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 16), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 24) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 24), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 32) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 32), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 40) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 40), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 48) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 48), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 56) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 56), ':')"/>
							</xsl:if>
						</xsl:when>

						<xsl:when test="$piece-before = 'B'">
							<!-- Diagonals every which way. -->
							<xsl:if test="(($my-finish + 7) mod 8) &#62; 0 and ($my-finish + 7) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 7), ':')"/>

								<xsl:if test="(($my-finish + 14) mod 8) &#62; 0 and ($my-finish + 14) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 14), ':')"/>

									<xsl:if test="(($my-finish + 21) mod 8) &#62; 0 and ($my-finish + 21) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 21), ':')"/>

										<xsl:if test="(($my-finish + 28) mod 8) &#62; 0 and ($my-finish + 28) &#60; 65">
											<xsl:value-of select="concat(string($my-finish + 28), ':')"/>

											<xsl:if test="(($my-finish + 35) mod 8) &#62; 0 and ($my-finish + 35) &#60; 65">
												<xsl:value-of select="concat(string($my-finish + 35), ':')"/>

												<xsl:if test="(($my-finish + 42) mod 8) &#62; 0 and ($my-finish + 42) &#60; 65">
													<xsl:value-of select="concat(string($my-finish + 42), ':')"/>

													<xsl:if test="(($my-finish + 49) mod 8) &#62; 0 and ($my-finish + 49) &#60; 65">
														<xsl:value-of select="concat(string($my-finish + 49), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish + 9) mod 8) &#62; 0 and ($my-finish + 9) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 9), ':')"/>

								<xsl:if test="(($my-finish + 18) mod 8) &#62; 0 and ($my-finish + 18) &#60; 65">
									<xsl:value-of select="concat(string($my-finish + 18), ':')"/>

									<xsl:if test="(($my-finish + 27) mod 8) &#62; 0 and ($my-finish + 27) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 27), ':')"/>

										<xsl:if test="(($my-finish + 36) mod 8) &#62; 0 and ($my-finish + 36) &#60; 65">
											<xsl:value-of select="concat(string($my-finish + 36), ':')"/>

											<xsl:if test="(($my-finish + 45) mod 8) &#62; 0 and ($my-finish + 45) &#60; 65">
												<xsl:value-of select="concat(string($my-finish + 45), ':')"/>

												<xsl:if test="(($my-finish + 54) mod 8) &#62; 0 and ($my-finish + 54) &#60; 65">
													<xsl:value-of select="concat(string($my-finish + 54), ':')"/>

													<xsl:if test="(($my-finish + 63) mod 8) &#62; 0 and ($my-finish + 63) &#60; 65">
														<xsl:value-of select="concat(string($my-finish + 63), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish - 7) mod 8) &#62; 0 and ($my-finish - 7) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 7), ':')"/>

								<xsl:if test="(($my-finish - 14) mod 8) &#62; 0 and ($my-finish - 14) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 14), ':')"/>

									<xsl:if test="(($my-finish - 21) mod 8) &#62; 0 and ($my-finish - 21) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 21), ':')"/>

										<xsl:if test="(($my-finish - 28) mod 8) &#62; 0 and ($my-finish - 28) &#62; 0">
											<xsl:value-of select="concat(string($my-finish - 28), ':')"/>

											<xsl:if test="(($my-finish - 35) mod 8) &#62; 0 and ($my-finish - 35) &#62; 0">
												<xsl:value-of select="concat(string($my-finish - 35), ':')"/>

												<xsl:if test="(($my-finish - 42) mod 8) &#62; 0 and ($my-finish - 42) &#62; 0">
													<xsl:value-of select="concat(string($my-finish - 42), ':')"/>

													<xsl:if test="(($my-finish - 49) mod 8) &#62; 0 and ($my-finish - 49) &#62; 0">
														<xsl:value-of select="concat(string($my-finish - 49), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>

							<xsl:if test="(($my-finish - 9) mod 8) &#62; 0 and ($my-finish - 9) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 9), ':')"/>

								<xsl:if test="(($my-finish - 18) mod 8) &#62; 0 and ($my-finish - 18) &#62; 0">
									<xsl:value-of select="concat(string($my-finish - 18), ':')"/>

									<xsl:if test="(($my-finish - 27) mod 8) &#62; 0 and ($my-finish - 27) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 27), ':')"/>

										<xsl:if test="(($my-finish - 36) mod 8) &#62; 0 and ($my-finish - 36) &#62; 0">
											<xsl:value-of select="concat(string($my-finish - 36), ':')"/>

											<xsl:if test="(($my-finish - 45) mod 8) &#62; 0 and ($my-finish - 45) &#62; 0">
												<xsl:value-of select="concat(string($my-finish - 45), ':')"/>

												<xsl:if test="(($my-finish - 54) mod 8) &#62; 0 and ($my-finish - 54) &#62; 0">
													<xsl:value-of select="concat(string($my-finish - 54), ':')"/>

													<xsl:if test="(($my-finish - 63) mod 8) &#62; 0 and ($my-finish - 63) &#62; 0">
														<xsl:value-of select="concat(string($my-finish - 63), ':')"/>
													</xsl:if>
												</xsl:if>
											</xsl:if>
										</xsl:if>
									</xsl:if>
								</xsl:if>
							</xsl:if>
						</xsl:when>

						<xsl:when test="$piece-before = 'N'">
							<xsl:if test="($my-finish + 6) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 10) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 10), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 15) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 15), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish + 17) &#60; 65">
								<xsl:value-of select="concat(string($my-finish + 17), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 6) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 6), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 10) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 10), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 15) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 15), ':')"/>
							</xsl:if>

							<xsl:if test="($my-finish - 17) &#62; 0">
								<xsl:value-of select="concat(string($my-finish - 17), ':')"/>
							</xsl:if>
						</xsl:when>

						<xsl:when test="$piece-before = 'P'">
							<xsl:choose>
								<!-- White is addition, black is subtraction. -->
								<xsl:when test="$piece = 'p'">
									<xsl:if test="($my-finish + 7) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 7), ':')"/>
									</xsl:if>

									<xsl:if test="($my-finish + 8) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 8), ':')"/>
									</xsl:if>

									<xsl:if test="($my-finish + 9) &#60; 65">
										<xsl:value-of select="concat(string($my-finish + 9), ':')"/>
									</xsl:if>

									<!-- At start, may move two spaces. -->
									<xsl:if test="($my-finish mod 8 = 2)">
										<xsl:value-of select="concat(string($my-finish + 16), ':')"/>
									</xsl:if>
								</xsl:when>

								<xsl:otherwise>
									<xsl:if test="($my-finish - 7) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 7), ':')"/>
									</xsl:if>

									<xsl:if test="($my-finish - 8) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 8), ':')"/>
									</xsl:if>

									<xsl:if test="($my-finish - 9) &#62; 0">
										<xsl:value-of select="concat(string($my-finish - 9), ':')"/>
									</xsl:if>

									<xsl:if test="($my-finish mod 8 = 7)">
										<xsl:value-of select="concat(string($my-finish - 16), ':')"/>
									</xsl:if>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>
					</xsl:choose>
				</xsl:variable>

				<xsl:call-template name="find-piece">
					<xsl:with-param name="chessboard" select="$chessboard"/>
					<xsl:with-param name="piece" select="$piece"/>
					<xsl:with-param name="coordinates" select="$coordinates"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="substring-position">
		<xsl:param name="string" select="''"/>
		<xsl:param name="substring" select="''"/>

		<xsl:choose>
			<xsl:when test="string-length(substring-after($string, $substring)) = 0">
				<xsl:value-of select="7"/>
			</xsl:when>

			<xsl:otherwise>
				<xsl:value-of select="string(string-length($string) - string-length(substring-after($string, $substring)))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="find-piece">
		<xsl:param name="chessboard" select="''"/>
		<xsl:param name="piece" select="''"/>
		<xsl:param name="coordinates" select="''"/>

		<xsl:variable name="my-coordinates">
			<xsl:choose>
				<xsl:when test="substring($coordinates, string-length($coordinates) - 1, 1) = ':'"><xsl:value-of select="substring($coordinates, 1, string-length($coordinates) - 1)"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$coordinates"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="my-coordinates-before">
			<xsl:choose>
				<xsl:when test="contains($my-coordinates, ':')"><xsl:value-of select="substring-before($my-coordinates, ':')"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$my-coordinates"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="my-coordinates-file">
			<xsl:choose>
				<xsl:when test="number($my-coordinates-before) &#62; 0 and number($my-coordinates-before) &#60; 9">1</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 8 and number($my-coordinates-before) &#60; 17">2</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 16 and number($my-coordinates-before) &#60; 25">3</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 24 and number($my-coordinates-before) &#60; 33">4</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 32 and number($my-coordinates-before) &#60; 41">5</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 40 and number($my-coordinates-before) &#60; 49">6</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 48 and number($my-coordinates-before) &#60; 57">7</xsl:when>
				<xsl:when test="number($my-coordinates-before) &#62; 56 and number($my-coordinates-before) &#60; 65">8</xsl:when>
			</xsl:choose>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="contains(substring($chessboard, number($my-coordinates-before), 1), $piece)">
				<xsl:value-of select="concat(substring('abcdefgh', number($my-coordinates-file), 1), number($my-coordinates-before) mod 8)"/>
			</xsl:when>

			<xsl:otherwise>
				<xsl:if test="contains($my-coordinates, ':')">
					<xsl:call-template name="find-piece">
						<xsl:with-param name="chessboard" select="$chessboard"/>
						<xsl:with-param name="piece" select="$piece"/>
						<xsl:with-param name="coordinates" select="substring-after($my-coordinates, ':')"/>
					</xsl:call-template>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- ****************************************************** -->
	<!--  Draws two diagrams for half-moves.                    -->
	<!--  Parameters:                                           -->
	<!--    $move-white - first half-move state;                -->
	<!--    $move-black - second half-move state.               -->
	<!-- ****************************************************** -->

	<xsl:template name="render-move">
		<xsl:param name="move-white"/>
		<xsl:param name="move-black"/>

		<xsl:variable name="move-number">
			<xsl:number count="move" format="1. "/>
		</xsl:variable>

		<table class="chess">
			<caption>
				<xsl:value-of select="$move-number"/>
				<xsl:value-of select="white"/>

				<xsl:if test="not(following-sibling::move) and not(black)">
					<xsl:text> </xsl:text>
					<xsl:value-of select="ancestor::chessgame/@result"/>
				</xsl:if>
			</caption>

			<xsl:call-template name="render-chessboard">
					<xsl:with-param name="chessboard" select="$move-white"/>
			</xsl:call-template>
		</table>

		<table class="chess">
			<xsl:if test="$move-black != ''">
				<caption>
					<xsl:value-of select="$move-number"/>
					<xsl:text>&#38;#x2026; </xsl:text>
					<xsl:value-of select="black"/>

					<xsl:if test="not(following-sibling::move)">
						<xsl:text> </xsl:text>
						<strong><xsl:value-of select="ancestor::chessgame/@result"/></strong>
					</xsl:if>
				</caption>

				<xsl:call-template name="render-chessboard">
					<xsl:with-param name="chessboard" select="$move-black"/>
				</xsl:call-template>
			</xsl:if>
		</table>
	</xsl:template>

	<!-- ****************************************************** -->
	<!--  Draws a single diagram.                               -->
	<!--  Parameters:                                           -->
	<!--    $chessboard - board state.                          -->
	<!-- ****************************************************** -->

	<xsl:template name="render-chessboard">
		<xsl:param name="chessboard"/>

		<tbody>
			<xsl:call-template name="render-chessboard-row">
				<xsl:with-param name="chessboard" select="$chessboard"/>
			</xsl:call-template>
		</tbody>
	</xsl:template>


	<!-- ****************************************************** -->
	<!--  Recursive template: draws a specified chessboard row. -->
	<!--  Calls itself for the next row. Note that rows are     -->
	<!--  listed in descending order: the first row will be     -->
	<!--  drawn at the bottom.                                  -->
	<!--  Parameters:                                           -->
	<!--    $chessboard - board state;                          -->
	<!--    $row-number - row being drawn.                      -->
	<!-- ****************************************************** -->

	<xsl:template name="render-chessboard-row">
		<xsl:param name="chessboard"/>
		<xsl:param name="row-number" select="8"/>

		<tr>
			<xsl:call-template name="render-chessboard-cell">
				<xsl:with-param name="row-number" select="$row-number"/>
				<xsl:with-param name="count" select="1"/>
				<xsl:with-param name="row-content">
					<xsl:value-of select="substring($chessboard, $row-number * 8 - 7, 8)"/>
				</xsl:with-param>
			</xsl:call-template>
		</tr>

		<xsl:if test="$row-number &#62; 1">
			<!-- recurse if there are more rows -->
			<xsl:call-template name="render-chessboard-row">
				<xsl:with-param name="chessboard" select="$chessboard"/>
				<xsl:with-param name="row-number" select="$row-number - 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- ****************************************************** -->
	<!--  Recursive template: draws a specified board cell and  -->
	<!--  calls itself for the next cell.                       -->
	<!--  Parameters:                                           -->
	<!--    $row-number - current row;                          -->
	<!--    $col-number - current column;                       -->
	<!--    $row-content - current state of figures in the row. -->
	<!-- ****************************************************** -->

	<xsl:template name="render-chessboard-cell">
		<xsl:param name="col-number" select="1"/>
		<xsl:param name="row-number"/>
		<xsl:param name="row-content"/>

		<xsl:variable name="cellref">
			<xsl:choose>
				<xsl:when test="$col-number = 1"><xsl:value-of select="concat('a', string($row-number))" /></xsl:when>
				<xsl:when test="$col-number = 2"><xsl:value-of select="concat('b', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 3"><xsl:value-of select="concat('c', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 4"><xsl:value-of select="concat('d', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 5"><xsl:value-of select="concat('e', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 6"><xsl:value-of select="concat('f', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 7"><xsl:value-of select="concat('g', string($row-number))"/></xsl:when>
				<xsl:when test="$col-number = 8"><xsl:value-of select="concat('h', string($row-number))"/></xsl:when>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="cell-content" select="substring($row-content, $col-number, 1)"/>

		<td class="{$cellref}">
			<xsl:call-template name="piece-image">
				<xsl:with-param name="piece-name" select="$cell-content"/>
			</xsl:call-template>
		</td>

		<xsl:if test="$col-number &#60; 8">
			<!-- recurse to the next cell -->
			<xsl:call-template name="render-chessboard-cell">
				<xsl:with-param name="row-number"  select="$row-number"/>
				<xsl:with-param name="col-number"  select="$col-number + 1"/>
				<xsl:with-param name="row-content" select="$row-content"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- ****************************************************** -->
	<!--  Produce a reference to the image of the figure.       -->
	<!--  Parameters:                                           -->
	<!--    $piece-name - piece letter code;                    -->
	<!--    $cell-background = b | w - cell color.              -->
	<!-- The image is taken from a chess font, referenced by    -->
	<!-- its Unicode value (yes, there are chess figures in the -->
	<!-- Unicode!)                                              -->
	<!-- ****************************************************** -->

	<xsl:template name="piece-image">
		<xsl:param name="piece-name"/>

		<xsl:choose>
			<xsl:when test="$piece-name='K'">&#38;#x2654;</xsl:when>
			<xsl:when test="$piece-name='Q'">&#38;#x2655;</xsl:when>
			<xsl:when test="$piece-name='R'">&#38;#x2656;</xsl:when>
			<xsl:when test="$piece-name='B'">&#38;#x2657;</xsl:when>
			<xsl:when test="$piece-name='N'">&#38;#x2658;</xsl:when>
			<xsl:when test="$piece-name='P'">&#38;#x2659;</xsl:when>
			<xsl:when test="$piece-name='k'">&#38;#x265A;</xsl:when>
			<xsl:when test="$piece-name='q'">&#38;#x265B;</xsl:when>
			<xsl:when test="$piece-name='r'">&#38;#x265C;</xsl:when>
			<xsl:when test="$piece-name='b'">&#38;#x265D;</xsl:when>
			<xsl:when test="$piece-name='n'">&#38;#x265E;</xsl:when>
			<xsl:when test="$piece-name='p'">&#38;#x265F;</xsl:when>
			<xsl:otherwise>&#38;#160;</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
