# Knight's Tour

This is a stylesheet whose output is a knight's tour of the chessboard (the knight can start on any square, and has to visit each square exactly once). This was published as an example stylesheet in my book XSLT Programmers Reference (Wrox Press) but has been completely reworked so it now makes extensive use of XSLT 2.0 and XPath 2.0 features. It is worth studying this stylesheet as an introduction to the use of XSLT 2.0 functions, and XPath 2.0 `for` and `if` expressions.

The stylesheet can be found in the file `samples\tour.xsl`. It takes no input from the source document, so you can supply any XML document as input, for example the stylesheet itself. You can run this example with a command of the form:

java -jar saxon7.jar -a samples\styles\tour.xsl start=e5 >tour.html

When you display the resulting HTML file in your browser it should look like this:

Knight's tour starting at e5

6 | 3 | 8 | 21 | 42 | 51 | 18 | 39
9 | 22 | 5 | 2 | 19 | 40 | 43 | 50
4 | 7 | 20 | 41 | 52 | 49 | 38 | 17
23 | 10 | 27 | 48 | 1 | 44 | 55 | 60
26 | 31 | 24 | 53 | 56 | 61 | 16 | 37
11 | 28 | 47 | 34 | 45 | 54 | 59 | 64
32 | 25 | 30 | 13 | 62 | 57 | 36 | 15
29 | 12 | 33 | 46 | 35 | 14 | 63 | 58

Expand | Next