1<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
2<xsl:output encoding="utf-8"/>
3<xsl:template match="towerheight">
4    <towersequence>
5        <xsl:call-template name="print">
6            <xsl:with-param name="height1" select="."/>
7            <xsl:with-param name="height2" select="0"/>
8            <xsl:with-param name="height3" select="0"/>
9        </xsl:call-template>
10        <xsl:call-template name="transferstack">
11            <xsl:with-param name="source" select="1"/>
12            <xsl:with-param name="target" select="2"/>
13            <xsl:with-param name="spare" select="3"/>
14            <xsl:with-param name="height1" select="."/>
15            <xsl:with-param name="height2" select="0"/>
16            <xsl:with-param name="height3" select="0"/>
17            <xsl:with-param name="howmany" select="."/>
18        </xsl:call-template>
19    </towersequence>
20</xsl:template>
21
22<xsl:template name="transferstack">
23    <xsl:param name="source"/>
24    <xsl:param name="target"/>
25    <xsl:param name="spare"/>
26    <xsl:param name="height1"/>
27    <xsl:param name="height2"/>
28    <xsl:param name="height3"/>
29    <xsl:param name="howmany"/>
30    <xsl:if test="$howmany > 0">
31        <xsl:call-template name="transferstack">
32            <xsl:with-param name="source" select="$source"/>
33            <xsl:with-param name="target" select="$spare"/>
34            <xsl:with-param name="spare" select="$target"/>
35            <xsl:with-param name="height1" select="$height1"/>
36            <xsl:with-param name="height2" select="$height2"/>
37            <xsl:with-param name="height3" select="$height3"/>
38            <xsl:with-param name="howmany" select="$howmany - 1"/>
39        </xsl:call-template>
40        <xsl:call-template name="print">
41            <xsl:with-param name="height1"
42                select="($source = 1) * ($height1 - $howmany) + ($target = 1) * ($height1 + 1) + ($spare = 1) * ($height1 + $howmany - 1)"/>
43            <xsl:with-param name="height2"
44                select="($source = 2) * ($height2 - $howmany) + ($target = 2) * ($height2 + 1) + ($spare = 2) * ($height2 + $howmany - 1)"/>
45            <xsl:with-param name="height3"
46                select="($source = 3) * ($height3 - $howmany) + ($target = 3) * ($height3 + 1) + ($spare = 3) * ($height3 + $howmany - 1)"/>
47        </xsl:call-template>
48
49        <xsl:call-template name="transferstack">
50            <xsl:with-param name="source" select="$spare"/>
51            <xsl:with-param name="target" select="$target"/>
52            <xsl:with-param name="spare" select="$source"/>
53            <xsl:with-param name="height1"
54                select="($source = 1) * ($height1 - $howmany) + ($target = 1) * ($height1 + 1) + ($spare = 1) * ($height1 + $howmany - 1)"/>
55            <xsl:with-param name="height2"
56                select="($source = 2) * ($height2 - $howmany) + ($target = 2) * ($height2 + 1) + ($spare = 2) * ($height2 + $howmany - 1)"/>
57            <xsl:with-param name="height3"
58                select="($source = 3) * ($height3 - $howmany) + ($target = 3) * ($height3 + 1) + ($spare = 3) * ($height3 + $howmany - 1)"/>
59            <xsl:with-param name="howmany" select="$howmany - 1"/>
60        </xsl:call-template>
61    </xsl:if>
62</xsl:template>
63
64<xsl:template name="print">
65    <xsl:param name="height1"/>
66    <xsl:param name="height2"/>
67    <xsl:param name="height3"/>
68    <tower>
69        <pole height="{$height1}"/>
70        <pole height="{$height2}"/>
71        <pole height="{$height3}"/>
72    </tower>
73</xsl:template>
74
75</xsl:stylesheet>
76