<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" version="1.0">
    <xsl:import href="trig_funcs.xsl"/>
    <!-- params -->
    <xsl:param name="page-width" select="210"/>
    <xsl:param name="page-height" select="297"/>
    <xsl:param name="left-part-width" select="12"/>
    <xsl:param name="page-margin" select="5"/>
    <xsl:param name="background-color" select="'rgb(214,214,214)'"/>
    <xsl:param name="axis-color" select="'rgb(0,0,0)'"/>
    <xsl:param name="bar-color" select="'blue'"/>
    <xsl:param name="grid-color" select="'rgb(0,0,0)'"/>
    <xsl:param name="outline-color" select="'rgb(0,0,0)'"/>
    <xsl:param name="empty-color" select="'rgb(240,255,240)'"/>
    <xsl:param name="growing-color" select="'red'"/>
    <xsl:param name="falling-color" select="'blue'"/>
    <xsl:param name="circle-color" select="'green'"/>
    <xsl:param name="stick-color" select="'yellow'"/>
    <xsl:param name="internet-color" select="'yellow'"/>
    <xsl:param name="phone-color" select="'rgb(40%, 80%, 40%)'"/>
    <xsl:param name="electricity-color" select="'#9999ff'"/>
    <xsl:param name="legend-width" select="55"/>
    <!-- variables -->
    <!-- *********************************************************** -->
    <!-- ****************** templates ****************************** -->
    <!-- *********************************************************** -->
    <xsl:template match="report">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="order" page-width="{$page-width}mm" page-height="{$page-height}mm">
                    <fo:region-body margin="5mm"/>
                    <fo:region-before extent="5mm"/>
                    <fo:region-after extent="5mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="order">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="11pt" font-family="Times" margin-right="10mm">
                        <fo:table>
                            <fo:table-column column-width="100%"/>
                            <fo:table-body>
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block text-align="center" font-weight="bold"
                                            font-size="14pt" border-bottom="2pt solid black">
                                            <xsl:apply-templates select="subject"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block padding-top="2mm">
                                            <xsl:apply-templates select="introduction"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block>
                                            <fo:table>
                                                <fo:table-column column-width="100%"/>
                                                <fo:table-body>
                                                  <xsl:apply-templates select="part | summary"/>
                                                </fo:table-body>
                                            </fo:table>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block text-align="right">
                                            <xsl:apply-templates select="author"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template match="part">
        <fo:table-row>
            <fo:table-cell>
                <fo:block padding-top="5mm">
                    <xsl:apply-templates select="description"/>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
        <fo:table-row>
            <fo:table-cell>
                <fo:block padding-top="5mm">
                    <fo:table>
                        <fo:table-column column-width="30mm"/>
                        <fo:table-column column-width="160mm"/>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block>
                                        <xsl:call-template name="put-table">
                                            <xsl:with-param name="entries" select="charges/entry"/>
                                        </xsl:call-template>
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block margin-left="10mm">
                                        <xsl:choose>
                                            <xsl:when test="charges/@for = 'electricity'">
                                                <xsl:call-template name="graph-bar">
                                                  <xsl:with-param name="graph-width" select="160"/>
                                                  <xsl:with-param name="graph-height" select="50"/>
                                                  <xsl:with-param name="node-set" select="charges/entry"/>
                                                  <xsl:with-param name="scale-unit" select="charges/scale_unit"/>
                                                </xsl:call-template>
                                            </xsl:when>
                                            <xsl:when test="charges/@for = 'internet'">
                                                <xsl:call-template name="graph">
                                                  <xsl:with-param name="graph-width" select="160"/>
                                                  <xsl:with-param name="graph-height" select="50"/>
                                                  <xsl:with-param name="node-set" select="charges/entry"/>
                                                  <xsl:with-param name="scale-unit" select="charges/scale_unit"/>
                                                </xsl:call-template>
                                            </xsl:when>
                                            <xsl:when test="charges/@for = 'phone'">
                                                <xsl:call-template name="graph-bar-horizontal">
                                                  <xsl:with-param name="graph-width" select="160"/>
                                                  <xsl:with-param name="graph-height" select="50"/>
                                                  <xsl:with-param name="node-set" select="charges/entry"/>
                                                  <xsl:with-param name="scale-unit" select="charges/scale_unit"/>
                                                </xsl:call-template>
                                            </xsl:when>
                                        </xsl:choose>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template match="summary">
        <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <xsl:apply-templates/>
                </fo:block>
                <fo:block>
                    <xsl:call-template name="put-pie">
                        <xsl:with-param name="total-electricity" select="sum(//part/charges[@for = 'electricity']/entry/amount)"/>
                        <xsl:with-param name="total-internet" select="sum(//part/charges[@for = 'internet']/entry/amount)"/>
                        <xsl:with-param name="total-phone" select="sum(//part/charges[@for = 'phone']/entry/amount)"/>
                        <xsl:with-param name="radius" select="20"/>
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template match="month">
        <xsl:call-template name="month-names">
            <xsl:with-param name="month" select="."/>
        </xsl:call-template>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="put-table">
        <xsl:param name="entries"/>
        <fo:block display-align="center" width="100%" font-size="9pt">
            <fo:table>
                <fo:table-column column-width="100%"/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell display-align="center" width="100%">
                            <fo:block-container width="100%" display-align="center">
                                <fo:table>
                                    <fo:table-column/>
                                    <fo:table-column/>
                                    <fo:table-body>
                                        <fo:table-row>
                                            <fo:table-cell border="1pt solid black" padding="0.5mm 0.5mm 0.5mm 2mm">
                                                <fo:block>
                                                  <fo:inline font-weight="bold"> Month, year </fo:inline>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell border="1pt solid black" padding="0.5mm 0.5mm 0.5mm 2mm">
                                                <fo:block>
                                                  <fo:inline font-weight="bold"> Amount </fo:inline>
                                                </fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                        <xsl:for-each select="$entries">
                                            <fo:table-row>
                                                <fo:table-cell border="1pt solid black" padding="0.5mm 0.5mm 0.5mm 2mm">
                                                  <fo:block>
                                                  <xsl:call-template name="month-names">
                                                  <xsl:with-param name="month" select="month"/>
                                                  </xsl:call-template>,&#xA0;
                                                  <xsl:value-of select="year"/>
                                                  </fo:block>
                                                </fo:table-cell>
                                                <fo:table-cell border="1pt solid black" padding="0.5mm 0.5mm 0.5mm 2mm">
                                                  <fo:block>
                                                  <xsl:value-of select="amount"/>
                                                  </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </xsl:for-each>
                                    </fo:table-body>
                                </fo:table>
                            </fo:block-container>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="graph-bar">
        <xsl:param name="graph-width"/>
        <xsl:param name="graph-height"/>
        <xsl:param name="node-set"/>
        <xsl:param name="scale-unit"/>
        <xsl:variable name="number-of-entries" select="count($node-set)"/>
        <xsl:variable name="max-amount">
            <xsl:call-template name="get-maximum">
                <xsl:with-param name="node-set" select="$node-set/amount"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="left-part-width" select="10"/>
        <xsl:variable name="right-part-width" select="$graph-width - $left-part-width"/>
        <xsl:variable name="x-scale-height" select="5"/>
        <xsl:variable name="available-height" select="$graph-height - $x-scale-height"/>
        <xsl:variable name="total-units" select="floor($max-amount div $scale-unit)"/>
        <xsl:variable name="month-width" select="floor($right-part-width div $number-of-entries)"/>
        <fo:table>
            <fo:table-column column-width="{$left-part-width}mm"/>
            <fo:table-column column-width="{$right-part-width}mm"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell height="{$available-height}mm">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$left-part-width}mm" height="{$available-height}mm">
                                    <xsl:call-template name="draw-scale">
                                        <xsl:with-param name="qty" select="$total-units"/>
                                        <xsl:with-param name="unit" select="$scale-unit"/>
                                        <xsl:with-param name="max-amount" select="$max-amount"/>
                                        <xsl:with-param name="available-height" select="$available-height"/>
                                        <xsl:with-param name="total-units" select="$total-units"/>
                                    </xsl:call-template>
                                    <svg:line x1="0" y1="{$available-height - 0.5}mm"
                                        x2="{$left-part-width}mm" y2="{$available-height - 0.5}mm" style="stroke:{$axis-color};stroke-width:4"/>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell height="{$available-height}mm" background-color="{$background-color}">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$right-part-width}mm" height="{$available-height}mm">
                                    <xsl:call-template name="draw-graph">
                                        <xsl:with-param name="entries" select="$node-set"/>
                                        <xsl:with-param name="idx" select="$number-of-entries"/>
                                        <xsl:with-param name="max-amount" select="$max-amount"/>
                                        <xsl:with-param name="available-height" select="$available-height"/>
                                        <xsl:with-param name="available-width" select="$right-part-width"/>
                                        <xsl:with-param name="month-width" select="$month-width"/>
                                    </xsl:call-template>
                                    <svg:line x1="0" y1="{$available-height - 0.5}mm"
                                        x2="{$right-part-width}mm" y2="{$available-height - 0.5}mm" style="stroke:{$axis-color};stroke-width:4"/>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
                <fo:table-row>
                    <fo:table-cell height="{$x-scale-height}mm"/>
                    <fo:table-cell height="{$x-scale-height}mm">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$right-part-width}mm" height="9mm">
                                    <xsl:call-template name="draw-months">
                                        <xsl:with-param name="entries" select="$node-set"/>
                                        <xsl:with-param name="idx" select="$number-of-entries"/>
                                        <xsl:with-param name="max-label-length" select="6"/>
                                        <xsl:with-param name="number-of-entries" select="$number-of-entries"/>
                                        <xsl:with-param name="right-part-width" select="$right-part-width"/>
                                    </xsl:call-template>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-scale">
        <xsl:param name="qty"/>
        <xsl:param name="unit"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="available-height"/>
        <xsl:param name="total-units"/>
        <xsl:variable name="unit-len" select="$unit div $max-amount * $available-height"/>
        <xsl:choose>
            <xsl:when test="number($qty) = 0"/>
            <xsl:otherwise>
                <svg:text style="fill:black;" y="{$available-height - $qty * $unit-len + 3}mm" x="0">
                    <xsl:value-of select="$qty * $unit"/>
                </svg:text>
                <!-- <svg:text style="fill:black;" y="12" x="{($total-units - $qty + 1) * $unit-len - 1.6 * string-length(string(($total-units - $qty + 1) * $unit))}mm">
                    <xsl:value-of select="($total-units - $qty + 1) * $unit"/>
                </svg:text> -->
                <svg:line x1="0" y1="{$available-height - $qty * $unit-len}mm"
                    x2="{$left-part-width}mm" y2="{$available-height - $qty * $unit-len}mm" style="stroke:{$grid-color};stroke-width:2"/>
                <svg:line x1="{$left-part-width - 2.5}mm" y1="0" x2="{$left-part-width - 2.5}mm"
                    y2="{$available-height}mm" style="stroke:{$axis-color};stroke-width:4"/>
                <xsl:call-template name="draw-scale">
                    <xsl:with-param name="qty" select="$qty - 1"/>
                    <xsl:with-param name="unit" select="$unit"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="available-height" select="$available-height"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-months">
        <xsl:param name="entries"/>
        <xsl:param name="idx"/>
        <xsl:param name="max-label-length"/>
        <xsl:param name="right-part-width"/>
        <!-- these values are calculated only once, when this template is called the first time! -->
        <xsl:param name="number-of-entries" select="count($entries)"/>
        <xsl:param name="month-width" select="floor($right-part-width div $number-of-entries)"/>
        <xsl:choose>
            <xsl:when test="number($idx) = 0"/>
            <xsl:otherwise>
                <xsl:variable name="current-entry" select="$entries[$number-of-entries - $idx + 1]"/>
                <xsl:variable name="short-month">
                    <xsl:call-template name="month-names">
                        <xsl:with-param name="month" select="$current-entry/month"/>
                        <xsl:with-param name="short-mode" select="'yes'"/>
                    </xsl:call-template>
                </xsl:variable>
                <svg:text style="fill:black;" y="12" x="{($number-of-entries - $idx + 1) * $month-width - 1.9 * $max-label-length}mm">
                    <xsl:value-of select="concat($short-month, '/', $current-entry/year)"/>
                </svg:text>
                <xsl:call-template name="draw-months">
                    <xsl:with-param name="entries" select="$entries"/>
                    <xsl:with-param name="idx" select="$idx - 1"/>
                    <xsl:with-param name="max-label-length" select="$max-label-length"/>
                    <xsl:with-param name="right-part-width" select="$right-part-width"/>
                    <xsl:with-param name="number-of-entries" select="$number-of-entries"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-graph">
        <xsl:param name="entries"/>
        <xsl:param name="idx"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="available-height"/>
        <xsl:param name="available-width"/>
        <xsl:param name="month-width"/>
        <xsl:choose>
            <xsl:when test="$idx = 1">
                <xsl:call-template name="draw-segment">
                    <xsl:with-param name="canvas-height" select="$available-height"/>
                    <xsl:with-param name="circle-radius" select="1"/>
                    <xsl:with-param name="current-x" select="$idx * $month-width"/>
                    <xsl:with-param name="current-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="neighbor-x" select="0"/>
                    <xsl:with-param name="neighbor-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="spline-color" select="$growing-color"/>
                    <xsl:with-param name="circle-color" select="$circle-color"/>
                    <xsl:with-param name="stick-color" select="$stick-color"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="line-color">
                    <xsl:choose>
                        <xsl:when test="number($entries[$idx - 1]/amount) &gt; $entries[$idx]/amount">
                            <xsl:value-of select="$falling-color"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$growing-color"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="draw-segment">
                    <xsl:with-param name="canvas-height" select="$available-height"/>
                    <xsl:with-param name="circle-radius" select="1"/>
                    <xsl:with-param name="current-x" select="$idx * $month-width"/>
                    <xsl:with-param name="current-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="neighbor-x" select="($idx - 1) * $month-width"/>
                    <xsl:with-param name="neighbor-y" select="$entries[$idx - 1]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="spline-color" select="$line-color"/>
                    <xsl:with-param name="circle-color" select="$circle-color"/>
                    <xsl:with-param name="stick-color" select="$stick-color"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
                <xsl:call-template name="draw-graph">
                    <xsl:with-param name="entries" select="$entries"/>
                    <xsl:with-param name="idx" select="$idx - 1"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="available-height" select="$available-height"/>
                    <xsl:with-param name="available-width" select="$available-width"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-segment">
        <xsl:param name="canvas-height"/>
        <xsl:param name="circle-radius"/>
        <xsl:param name="current-x"/>
        <xsl:param name="current-y"/>
        <xsl:param name="neighbor-x"/>
        <xsl:param name="neighbor-y"/>
        <xsl:param name="spline-color"/>
        <xsl:param name="circle-color"/>
        <xsl:param name="stick-color"/>
        <xsl:param name="month-width"/>
        <!-- spline 
        <svg:line x1="{$neighbor-x - $month-width div 2}mm" y1="{$canvas-height - $neighbor-y}mm" x2="{$current-x - $month-width div 2}mm"
            y2="{$canvas-height - $current-y}mm" style="stroke:{$spline-color};stroke-width:1"/> -->
        <!-- stick -->
        <!-- <svg:line x1="{$current-x - $month-width div 2}mm" y1="{$canvas-height - $current-y}mm" x2="{$current-x - $month-width div 2}mm"
            y2="{$canvas-height}mm" style="stroke:{$stick-color};stroke-width:1"/>
        <svg:circle cx="{$current-x - $month-width div 2}mm" cy="{$canvas-height - $current-y}mm" r="{$circle-radius}mm"
            stroke="{$circle-color}" stroke-width="1"/> -->
        <svg:rect x="{$neighbor-x + ($current-x - $neighbor-x) * 0.3}mm"
            y="{$canvas-height - $current-y}mm"
            width="{$current-x - $neighbor-x - ($current-x - $neighbor-x) * 0.3}mm"
            height="{$current-y}mm" style="fill:blue;stroke:black;stroke-width:1"/>
    </xsl:template>
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <xsl:template name="graph">
        <xsl:param name="graph-width"/>
        <xsl:param name="graph-height"/>
        <xsl:param name="node-set"/>
        <xsl:param name="scale-unit"/>
        <xsl:variable name="number-of-entries" select="count($node-set)"/>
        <xsl:variable name="max-amount">
            <xsl:call-template name="get-maximum">
                <xsl:with-param name="node-set" select="$node-set/amount"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="left-part-width" select="10"/>
        <xsl:variable name="right-part-width" select="$graph-width - $left-part-width"/>
        <xsl:variable name="x-scale-height" select="5"/>
        <xsl:variable name="available-height" select="$graph-height - $x-scale-height"/>
        <xsl:variable name="total-units" select="floor($max-amount div $scale-unit)"/>
        <xsl:variable name="month-width" select="floor($right-part-width div $number-of-entries)"/>
        <fo:table>
            <fo:table-column column-width="{$left-part-width}mm"/>
            <fo:table-column column-width="{$right-part-width}mm"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell height="{$available-height}mm">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$left-part-width}mm" height="{$available-height}mm">
                                    <xsl:call-template name="draw-scale">
                                        <xsl:with-param name="qty" select="$total-units"/>
                                        <xsl:with-param name="unit" select="$scale-unit"/>
                                        <xsl:with-param name="max-amount" select="$max-amount"/>
                                        <xsl:with-param name="available-height" select="$available-height"/>
                                        <xsl:with-param name="total-units" select="$total-units"/>
                                    </xsl:call-template>
                                    <svg:line x1="0" y1="{$available-height - 0.5}mm"
                                        x2="{$left-part-width}mm" y2="{$available-height - 0.5}mm" style="stroke:{$axis-color};stroke-width:4"/>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell height="{$available-height}mm" background-color="{$background-color}">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$right-part-width}mm" height="{$available-height}mm">
                                    <xsl:call-template name="draw-graph-lines">
                                        <xsl:with-param name="entries" select="$node-set"/>
                                        <xsl:with-param name="idx" select="$number-of-entries"/>
                                        <xsl:with-param name="max-amount" select="$max-amount"/>
                                        <xsl:with-param name="available-height" select="$available-height"/>
                                        <xsl:with-param name="available-width" select="$right-part-width"/>
                                        <xsl:with-param name="month-width" select="$month-width"/>
                                    </xsl:call-template>
                                    <svg:line x1="0" y1="{$available-height - 0.5}mm"
                                        x2="{$right-part-width}mm" y2="{$available-height - 0.5}mm" style="stroke:{$axis-color};stroke-width:4"/>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
                <fo:table-row>
                    <fo:table-cell height="{$x-scale-height}mm"/>
                    <fo:table-cell height="{$x-scale-height}mm">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$right-part-width}mm" height="9mm">
                                    <xsl:call-template name="draw-months">
                                        <xsl:with-param name="entries" select="$node-set"/>
                                        <xsl:with-param name="idx" select="$number-of-entries"/>
                                        <xsl:with-param name="max-label-length" select="6"/>
                                        <xsl:with-param name="number-of-entries" select="$number-of-entries"/>
                                        <xsl:with-param name="right-part-width" select="$right-part-width"/>
                                    </xsl:call-template>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-graph-lines">
        <xsl:param name="entries"/>
        <xsl:param name="idx"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="available-height"/>
        <xsl:param name="available-width"/>
        <xsl:param name="month-width"/>
        <xsl:choose>
            <xsl:when test="$idx = 1">
                <xsl:call-template name="draw-segment-line">
                    <xsl:with-param name="canvas-height" select="$available-height"/>
                    <xsl:with-param name="circle-radius" select="1"/>
                    <xsl:with-param name="current-x" select="$idx * $month-width"/>
                    <xsl:with-param name="current-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="neighbor-x" select="$idx * $month-width"/>
                    <xsl:with-param name="neighbor-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="spline-color" select="$growing-color"/>
                    <xsl:with-param name="circle-color" select="$circle-color"/>
                    <xsl:with-param name="stick-color" select="$stick-color"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="line-color">
                    <xsl:choose>
                        <xsl:when test="number($entries[$idx - 1]/amount) &gt; $entries[$idx]/amount">
                            <xsl:value-of select="$falling-color"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$growing-color"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="draw-segment-line">
                    <xsl:with-param name="canvas-height" select="$available-height"/>
                    <xsl:with-param name="circle-radius" select="1"/>
                    <xsl:with-param name="current-x" select="$idx * $month-width"/>
                    <xsl:with-param name="current-y" select="$entries[$idx]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="neighbor-x" select="($idx - 1) * $month-width"/>
                    <xsl:with-param name="neighbor-y" select="$entries[$idx - 1]/amount div $max-amount * $available-height"/>
                    <xsl:with-param name="spline-color" select="$line-color"/>
                    <xsl:with-param name="circle-color" select="$circle-color"/>
                    <xsl:with-param name="stick-color" select="$stick-color"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
                <xsl:call-template name="draw-graph-lines">
                    <xsl:with-param name="entries" select="$entries"/>
                    <xsl:with-param name="idx" select="$idx - 1"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="available-height" select="$available-height"/>
                    <xsl:with-param name="available-width" select="$available-width"/>
                    <xsl:with-param name="month-width" select="$month-width"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-segment-line">
        <xsl:param name="canvas-height"/>
        <xsl:param name="circle-radius"/>
        <xsl:param name="current-x"/>
        <xsl:param name="current-y"/>
        <xsl:param name="neighbor-x"/>
        <xsl:param name="neighbor-y"/>
        <xsl:param name="spline-color"/>
        <xsl:param name="circle-color"/>
        <xsl:param name="stick-color"/>
        <xsl:param name="month-width"/>
        <!-- spline -->
        <svg:line x1="{$neighbor-x - $month-width div 2}mm" y1="{$canvas-height - $neighbor-y}mm"
            x2="{$current-x - $month-width div 2}mm" y2="{$canvas-height - $current-y}mm" style="stroke:{$spline-color};stroke-width:1"/>
        <!-- stick -->
        <svg:line x1="{$current-x - $month-width div 2}mm" y1="{$canvas-height - $current-y}mm"
            x2="{$current-x - $month-width div 2}mm" y2="{$canvas-height}mm" style="stroke:{$stick-color};stroke-width:1"/>
        <svg:circle cx="{$current-x - $month-width div 2}mm" cy="{$canvas-height - $current-y}mm"
            r="{$circle-radius}mm" stroke="{$circle-color}" stroke-width="1"/>
    </xsl:template>
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <xsl:template name="graph-bar-horizontal">
        <xsl:param name="graph-width"/>
        <xsl:param name="graph-height"/>
        <xsl:param name="node-set"/>
        <xsl:param name="scale-unit"/>
        <xsl:variable name="number-of-entries" select="count($node-set)"/>
        <xsl:variable name="max-amount">
            <xsl:call-template name="get-maximum">
                <xsl:with-param name="node-set" select="$node-set/amount"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="left-part-width" select="15"/>
        <xsl:variable name="right-part-width" select="$graph-width - $left-part-width"/>
        <xsl:variable name="x-scale-height" select="5"/>
        <xsl:variable name="available-height" select="$graph-height - $x-scale-height"/>
        <xsl:variable name="total-units" select="floor($max-amount div $scale-unit)"/>
        <xsl:variable name="month-width" select="floor($right-part-width div $number-of-entries)"/>
        <xsl:variable name="unit-len" select="$scale-unit div $max-amount * $right-part-width"/>
        <fo:table>
            <fo:table-column column-width="{$left-part-width}mm"/>
            <fo:table-column column-width="{$right-part-width}mm"/>
            <fo:table-body>
                <xsl:call-template name="draw-bar-line">
                    <xsl:with-param name="entries" select="$node-set"/>
                    <xsl:with-param name="idx" select="$number-of-entries"/>
                    <xsl:with-param name="right-part-width" select="$right-part-width"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="unit-len" select="$unit-len"/>
                </xsl:call-template>
                <fo:table-row>
                    <fo:table-cell height="{$x-scale-height}mm"/>
                    <fo:table-cell height="{$x-scale-height}mm" border-top="2pt solid black">
                        <fo:block>
                            <fo:instream-foreign-object>
                                <svg:svg width="{$right-part-width}mm" height="{$x-scale-height}mm">
                                    <xsl:call-template name="draw-scale-horizontal">
                                        <xsl:with-param name="qty" select="$total-units"/>
                                        <xsl:with-param name="unit" select="$scale-unit"/>
                                        <xsl:with-param name="max-amount" select="$max-amount"/>
                                        <xsl:with-param name="right-part-width" select="$right-part-width"/>
                                        <xsl:with-param name="total-units" select="$total-units"/>
                                    </xsl:call-template>
                                    <svg:line x1="0" y1="{$available-height - 0.5}mm"
                                        x2="{$left-part-width}mm" y2="{$available-height - 0.5}mm" style="stroke:{$axis-color};stroke-width:4"/>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-scale-horizontal">
        <xsl:param name="qty"/>
        <xsl:param name="unit"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="right-part-width"/>
        <xsl:param name="total-units"/>
        <xsl:variable name="unit-len" select="$unit div $max-amount * $right-part-width"/>
        <xsl:choose>
            <xsl:when test="number($qty) = 0"/>
            <xsl:otherwise>
                <svg:text style="fill:black;" y="12" x="{($total-units - $qty + 1) * $unit-len - 1.8 * string-length(string(($total-units - $qty + 1) * $unit))}mm">
                    <xsl:value-of select="($total-units - $qty + 1) * $unit"/>
                </svg:text>
                <!--
                <svg:text style="fill:black;" y="{$available-height - $qty * $unit-len + 3}mm" x="0">
                    <xsl:value-of select="$qty * $unit"/>
                </svg:text>
                -->
                <!-- <svg:text style="fill:black;" y="12" x="{($total-units - $qty + 1) * $unit-len - 1.6 * string-length(string(($total-units - $qty + 1) * $unit))}mm">
                    <xsl:value-of select="($total-units - $qty + 1) * $unit"/>
                </svg:text> -->
                <!-- <svg:line x1="0" y1="{$available-height - $qty * $unit-len}mm"
                    x2="{$left-part-width}mm" y2="{$available-height - $qty * $unit-len}mm" style="stroke:{$grid-color};stroke-width:2"/>
                <svg:line x1="{$left-part-width - 2.5}mm" y1="0" x2="{$left-part-width - 2.5}mm"
                    y2="{$available-height}mm" style="stroke:{$axis-color};stroke-width:4"/> -->
                <xsl:call-template name="draw-scale-horizontal">
                    <xsl:with-param name="qty" select="$qty - 1"/>
                    <xsl:with-param name="unit" select="$unit"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="right-part-width" select="$right-part-width"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-bar-line">
        <xsl:param name="entries"/>
        <xsl:param name="idx"/>
        <xsl:param name="right-part-width"/>
        <xsl:param name="total-units"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="unit-len"/>
        <!-- these values are calculated only once, when this template is called the first time! -->
        <xsl:param name="number-of-entries" select="count($entries)"/>
        <xsl:choose>
            <xsl:when test="number($idx) = 0"/>
            <xsl:otherwise>
                <xsl:variable name="current-entry" select="$entries[$number-of-entries - $idx + 1]"/>
                <xsl:variable name="short-month">
                    <xsl:call-template name="month-names">
                        <xsl:with-param name="month" select="$current-entry/month"/>
                        <xsl:with-param name="short-mode" select="'yes'"/>
                    </xsl:call-template>
                </xsl:variable>
                <fo:table-row>
                    <fo:table-cell text-align="right" border-right="2pt solid black">
                        <fo:block>
                            <xsl:value-of select="concat($short-month, '/', $current-entry/year)"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell background-color="{$background-color}">
                        <fo:block>
                            <xsl:call-template name="bar">
                                <xsl:with-param name="charge" select="$current-entry/amount"/>
                                <xsl:with-param name="total-units" select="$total-units"/>
                                <xsl:with-param name="max-amount" select="$max-amount"/>
                                <xsl:with-param name="right-part-width" select="$right-part-width"/>
                                <xsl:with-param name="unit-len" select="$unit-len"/>
                            </xsl:call-template>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
                <xsl:call-template name="draw-bar-line">
                    <xsl:with-param name="entries" select="$entries"/>
                    <xsl:with-param name="idx" select="$idx - 1"/>
                    <xsl:with-param name="right-part-width" select="$right-part-width"/>
                    <xsl:with-param name="number-of-entries" select="$number-of-entries"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                    <xsl:with-param name="max-amount" select="$max-amount"/>
                    <xsl:with-param name="unit-len" select="$unit-len"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="bar">
        <xsl:param name="charge"/>
        <xsl:param name="total-units"/>
        <xsl:param name="max-amount"/>
        <xsl:param name="right-part-width"/>
        <xsl:param name="unit-len"/>
        <xsl:variable name="bar-length" select="$charge div $max-amount * $right-part-width"/>
        <fo:instream-foreign-object>
            <svg:svg width="{$right-part-width}mm" height="4mm">
                <!-- <xsl:call-template name="drawlines">
                    <xsl:with-param name="qty" select="$total-units"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                    <xsl:with-param name="unit-len" select="$unit-len"/>
                </xsl:call-template> -->
                <svg:line x1="0" y1="0" x2="{$bar-length}mm" y2="0" style="stroke:{$bar-color};stroke-width:70"/>
                <svg:line x1="0" y1="0" x2="0" y2="43" style="stroke:{$axis-color};stroke-width:4"/>
                <svg:text style="fill:black;" y="15" x="{$bar-length - $page-margin * 2}mm">
                    <xsl:value-of select="$charge"/>
                </svg:text>
            </svg:svg>
        </fo:instream-foreign-object>
    </xsl:template>
    <!-- ******************************************************** -->
    <!-- <xsl:variable name="unit-len" select="scale_unit div $max-amount * $right-part-width"/> -->
    <xsl:template name="drawlines">
        <xsl:param name="qty"/>
        <xsl:param name="total-units"/>
        <xsl:param name="unit-len"/>
        <xsl:choose>
            <xsl:when test="number($qty) = 0"/>
            <xsl:otherwise>
                <svg:line x1="{($total-units - $qty + 1) * $unit-len}mm" y1="0"
                    x2="{($total-units - $qty + 1) * $unit-len}mm" y2="63" style="stroke:{$grid-color};stroke-width:1"/>
                <xsl:call-template name="drawlines">
                    <xsl:with-param name="qty" select="$qty - 1"/>
                    <xsl:with-param name="total-units" select="$total-units"/>
                    <xsl:with-param name="unit-len" select="$unit-len"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <!-- ******************************************************** -->
    <xsl:template name="put-pie">
        <xsl:param name="total-electricity"/>
        <xsl:param name="total-internet"/>
        <xsl:param name="total-phone"/>
        <xsl:param name="radius"/>
        <xsl:variable name="grand-total" select="($total-electricity + $total-internet + $total-phone) * 3"/>
        <fo:block text-align="center">
            <fo:table>
                <fo:table-column column-width="70%"/>
                <fo:table-column column-width="30%"/>
                <fo:table-body>
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:variable name="offset-angle" select="90"/>
                            <xsl:variable name="begin-angle" select="$offset-angle - (($grand-total - $total-phone - $total-internet - $total-electricity) div $grand-total) * 360"/>
                            <xsl:variable name="internet-angle" select="$begin-angle - ($total-internet div $grand-total) * 360"/>
                            <xsl:variable name="phone-angle" select="$internet-angle - ($total-phone div $grand-total) * 360"/>
                            <xsl:variable name="electricity-angle" select="$phone-angle - ($total-electricity div $grand-total) * 360"/>
                            
                            <fo:instream-foreign-object>
                                <svg:svg width="{$radius * 2 + 1}mm" height="{$radius * 2 + 1}mm" viewBox="0 0 {$radius * 2 + 1} {$radius * 2 + 1}">
                                    <svg:circle cx="{$radius}" cy="{$radius}" r="{$radius}"
                                        stroke="{$outline-color}" stroke-width="0.2" fill="{$empty-color}"/>
                                    <xsl:call-template name="draw-pie-segment">
                                        <xsl:with-param name="start-angle" select="$begin-angle"/>
                                        <xsl:with-param name="end-angle" select="$internet-angle"/>
                                        <xsl:with-param name="radius" select="$radius"/>
                                        <xsl:with-param name="fill-color" select="$internet-color"/>
                                    </xsl:call-template>
                                    <xsl:call-template name="draw-pie-segment">
                                        <xsl:with-param name="start-angle" select="$internet-angle"/>
                                        <xsl:with-param name="end-angle" select="$phone-angle"/>
                                        <xsl:with-param name="radius" select="$radius"/>
                                        <xsl:with-param name="fill-color" select="$phone-color"/>
                                    </xsl:call-template>
                                    <xsl:call-template name="draw-pie-segment">
                                        <xsl:with-param name="start-angle" select="$phone-angle"/>
                                        <xsl:with-param name="end-angle" select="$electricity-angle"/>
                                        <xsl:with-param name="radius" select="$radius"/>
                                        <xsl:with-param name="fill-color" select="$electricity-color"/>
                                    </xsl:call-template>
                                </svg:svg>
                            </fo:instream-foreign-object>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block text-align="left">
                            <fo:table>
                                <fo:table-column column-width="2mm"/>
                                <fo:table-column column-width="30mm"/>
                                <fo:table-column column-width="10mm"/>
                                <fo:table-body>
                                    <fo:table-row height="4mm">
                                        <fo:table-cell>
                                            <xsl:call-template name="legend-color">
                                                <xsl:with-param name="color" select="$empty-color"/>
                                            </xsl:call-template>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                Other expences
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                <xsl:value-of select="round(($grand-total - $total-internet - $total-electricity - $total-phone) div $grand-total * 100)"/>%
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row height="4mm">
                                        <fo:table-cell>
                                            <xsl:call-template name="legend-color">
                                                <xsl:with-param name="color" select="$internet-color"/>
                                            </xsl:call-template>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                Internet expences
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                <xsl:value-of select="round(($total-internet) div $grand-total * 100)"/>%
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <xsl:call-template name="legend-color">
                                                <xsl:with-param name="color" select="$phone-color"/>
                                            </xsl:call-template>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                Phone expences
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                <xsl:value-of select="round(($total-phone) div $grand-total * 100)"/>%
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <xsl:call-template name="legend-color">
                                                <xsl:with-param name="color" select="$electricity-color"/>
                                            </xsl:call-template>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                Electricity expences
                                            </fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell>
                                            <fo:block>
                                                <xsl:value-of select="round(($total-electricity) div $grand-total * 100)"/>%
                                            </fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </fo:table-body>
                            </fo:table>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:block>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="legend-color">
        <xsl:param name="color"/>
        <fo:block>
            <fo:instream-foreign-object>
                <svg:svg width="4mm" height="4mm">
                    <svg:rect x1="2mm" y1="2mm" width="2mm" height="2mm" style="fill:{$color};stroke-width:0.1;stroke:black"/>
                </svg:svg>
            </fo:instream-foreign-object>
        </fo:block>
    </xsl:template>
    <!-- ******************************************************** -->
    <xsl:template name="draw-pie-segment">
        <xsl:param name="start-angle"/>
        <xsl:param name="end-angle"/>
        <xsl:param name="radius"/>
        <xsl:param name="fill-color"/>
        <xsl:variable name="start-sinus">
            <xsl:call-template name="sin">
                <xsl:with-param name="pX" select="$start-angle"/>
                <xsl:with-param name="pUnit" select="'deg'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="start-cosinus">
            <xsl:call-template name="cos">
                <xsl:with-param name="pX" select="$start-angle"/>
                <xsl:with-param name="pUnit" select="'deg'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="end-sinus">
            <xsl:call-template name="sin">
                <xsl:with-param name="pX" select="$end-angle"/>
                <xsl:with-param name="pUnit" select="'deg'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="end-cosinus">
            <xsl:call-template name="cos">
                <xsl:with-param name="pX" select="$end-angle"/>
                <xsl:with-param name="pUnit" select="'deg'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="x1" select="$radius * $start-cosinus + $radius"/>
        <xsl:variable name="y1" select="$radius - $radius * $start-sinus"/>
        <xsl:variable name="x2" select="$radius * $end-cosinus + $radius"/>
        <xsl:variable name="y2" select="$radius - $radius * $end-sinus"/>
        
        <svg:path d="M{$radius},{$radius} L{$x2},{$y2} A{$radius},{$radius} 0 0,0 {$x1},{$y1} z"
            fill="{$fill-color}" stroke="{$outline-color}" stroke-width="0.2"/>
    </xsl:template>
    <!-- ******************************************************** -->
    <!-- ************** auxiliary templates ********************* -->
    <!-- ******************************************************** -->
    <!-- ############## computational templates ################# -->
    <xsl:template name="get-maximum">
        <xsl:param name="node-set"/>
        <xsl:choose>
            <xsl:when test="not($node-set)">
                <xsl:message terminate="yes">There are no entries in the source document!</xsl:message>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="count($node-set) = 1">
                        <xsl:value-of select="$node-set[1]"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="max-of-rest">
                            <xsl:call-template name="get-maximum">
                                <xsl:with-param name="node-set" select="$node-set[position() &gt; 1]"/>
                            </xsl:call-template>
                        </xsl:variable>
                        <xsl:call-template name="max-of-two">
                            <xsl:with-param name="x" select="$node-set[1]"/>
                            <xsl:with-param name="y" select="$max-of-rest"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="max-of-two">
        <xsl:param name="x"/>
        <xsl:param name="y"/>
        <xsl:choose>
            <xsl:when test="number($x) &gt; number($y)">
                <xsl:value-of select="$x"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$y"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="month-names">
        <xsl:param name="month"/>
        <xsl:param name="short-mode" select="'no'"/>
        <xsl:variable name="month-number" select="number($month)"/>
        <xsl:variable name="result">
            <xsl:choose>
                <xsl:when test="$month-number = 1">January</xsl:when>
                <xsl:when test="$month-number = 2">February</xsl:when>
                <xsl:when test="$month-number = 3">March</xsl:when>
                <xsl:when test="$month-number = 4">April</xsl:when>
                <xsl:when test="$month-number = 5">May</xsl:when>
                <xsl:when test="$month-number = 6">June</xsl:when>
                <xsl:when test="$month-number = 7">July</xsl:when>
                <xsl:when test="$month-number = 8">August</xsl:when>
                <xsl:when test="$month-number = 9">September</xsl:when>
                <xsl:when test="$month-number = 10">October</xsl:when>
                <xsl:when test="$month-number = 11">November</xsl:when>
                <xsl:when test="$month-number = 12">December</xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$short-mode = 'yes'">
                <xsl:value-of select="substring($result, 1, 3)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$result"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
