An XSLT stylesheet for ChessGML

I like chess. I wanted to learn programming using the DOM. So the other day I set out to write a small programme that would take a chess game written using A. Saremba's ChessGML DTD and generate a view of the game for display within a browser. Saremba had already written a DSSSL stylesheet for formatting games as well as an XSLT stylesheet to generate an HTML page from a view, but results don't look so good and lack animation. So I thought that generating SVG would be a cool thing to do.

After a few hours writing Java code I realised that it would be easier to do it in XSLT. So I did it. To hell with the DOM! And it's so much easier! Maybe it's easier for me (being more familiar with XSLT), but in any case the code is much shorter. Here it is:

The Big Picture

Ideally one would write an XHTML file with embedded ChessGML markup, like this:

<?xml-stylesheet type="text/xsl" href="chess5.xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>...</head>
<body><h1>How I beat G. Kasparov</h1>
<h2>Game 1</h2>
<chess xmlns="http://www.chessgmlneedsanamespace.net">
...
</chess>
<body>
</html>

A browser receiving this would perform the XSLT transform described in chess5.xsl and which would replace the ChessGML markup by an SVG animation of the game, while leaving the HTML as is. Then the browser, recognising the SVG namespace would just display everything. Of course no browser does that yet. Bit of a shame, really.

How does it work?

Basically, the stylesheet first dumps the SVG fonts for the chess pieces and the initial board, and then generates SMIL animation markup for each move.

The tricky bit is that as each move (<m> in ChessGML) modifies the board, the stylesheet has to track its state, because ChessGML doesn't indicate when a certain piece has been captured.

XSLT doesn't allow such side effects. Therefore the board must be passed as a parameter to the template that handles <m>, with each <m> template calling the next recursively (instead of letting XSLT treat all <m>s as it pleases). The parameter passed is a string of 64 characters representing the state of each square.

Writing this I wonder if it it necessary to do it this way. It could be possible to figure out the sequence of each piece (especially when it is captured) by applying a single template to all<m>s. Since SMIL's<animate>s don't have to be in sequential order, it doesn't matter in what order the templates are called. Hmm. I'll have to think about this.A few months later: changing to a scheme where the movement of each piece is determined in turn is indeed possible and would avoid the board parameter and the sequential processing of moves. However, tracking when a piece is removed from the board or when it is promoted is not trivial with ChessGMLand would actually make the stylesheet more complex.

Planned Improvements

Acknowledgements

Thank yous go to Dean Jackson who convinced me not to be ashamed and publish this and who converted the chess fonts to SVG, Vincent Hardy who also encouraged me and suggested the scrolling move list, Thierry Kormann who provided assistance about SVG, and of course A. Saremba for designing ChessGML.

Max Froumentin (mf@w3.org).
Yes, this was done in my spare time.