Knight's Tour
This program is available in two forms: as an XSLT stylesheet tour.xsl
and as
an XQuery tour.xq
.
This is a program 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). The XSLT version was published as an example stylesheet in my book XSLT 2.0 Programmers Reference (Wrox Press) but has been completely reworked so it now makes extensive use of features in XSLT 2.0, XPath 2.0 and XQuery. It is worth studying the stylesheet and query as an introduction to the use of the new features in these languages. Comparing the two versions, it can be seen that they are very similar: the only differences are in the surface syntax of the two languages.
The stylesheet can be found in the file samples/styles/tour.xsl
, the query in
samples/query/tour.xq
. No source document is required.
You can run this example with Saxon on the Java platform using a command of the form:
java -jar saxon9.jar -it:main samples\styles\tour.xsl start=e5 >tour.htmlor
java -cp saxon9.jar net.sf.saxon.Query samples\query\tour.xq start=e5 >tour.htmlOn the .NET platform, the equivalent commands are:
Transform -it:main samples\styles\tour.xsl start=e5 >tour.htmlor
Query samples\query\tour.xq start=e5 >tour.htmlWhen 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 |