<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:variable name="RI" select="/All-list/DynamicXSL-list/DynamicXSL[DynamicXSLParamName='RefreshInterval']/DynamicXSLParamValue"/>
    <xsl:variable name="Length" select="/All-list/DynamicXSL-list/DynamicXSL[DynamicXSLParamName='Length']/DynamicXSLParamValue"/>
    <xsl:variable name="fromSec">
        <xsl:call-template name="Date2Sec">
            <xsl:with-param name="date" select="/All-list/DynamicXSL-list/DynamicXSL[DynamicXSLParamName='fromDate']/DynamicXSLParamValue"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="toSec">
        <xsl:call-template name="Date2Sec">
            <xsl:with-param name="date" select="/All-list/DynamicXSL-list/DynamicXSL[DynamicXSLParamName='toDate']/DynamicXSLParamValue"/>
        </xsl:call-template>
    </xsl:variable>
    <!-- ======================================================== -->
    <xsl:template match="/">
        var loadArr = new Array(<xsl:call-template name="basic"/>0)
    </xsl:template>
    <!-- ======================================================== -->
    <xsl:template name="basic">
        <xsl:param name="pos" select="1"/>
        <xsl:param name="sec" select="0"/>
        <xsl:for-each select="/All-list/DynReport-list/DynReport/ResultSet/Row/Columns">
            <xsl:sort select="Column[name='DT']/value"/>
            <xsl:if test="position()=$pos">
                <xsl:variable name="curSec">
                    <xsl:call-template name="Date2Sec">
                        <xsl:with-param name="date" select="Column[name='DT']/value"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="nextSec">
                    <xsl:if test="position()!=last()">
                        <xsl:call-template name="next">
                            <xsl:with-param name="pos" select="position()"/>
                        </xsl:call-template>
                    </xsl:if>
                    <xsl:if test="position()=last()">
                        <xsl:value-of select="$toSec"/>
                    </xsl:if>
                </xsl:variable>
                <xsl:variable name="emptySteps" select="substring-before(concat(($nextSec - $curSec) div $RI, '.'),'.')"/>
                <xsl:if test="$emptySteps=0">
                    <xsl:variable name="AvValue">
                        <xsl:call-template name="ManyDots">
                            <xsl:with-param name="start" select="$sec + $fromSec + position() * $RI"/>
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:if test="$AvValue!=''">
                        <xsl:value-of select="concat($AvValue, ',', $sec + $fromSec + position() * $RI, ',')"/>
                        <xsl:call-template name="basic">
                            <xsl:with-param name="pos" select="$pos + 1"/>
                            <xsl:with-param name="sec" select="$sec"/>
                        </xsl:call-template>
                    </xsl:if>
                    <xsl:if test="$AvValue=''">
                        <xsl:call-template name="basic">
                            <xsl:with-param name="pos" select="$pos + 1"/>
                            <xsl:with-param name="sec" select="$sec - $RI"/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:if>
                <xsl:if test="$emptySteps!=0">
                    <xsl:call-template name="for">
                        <xsl:with-param name="n" select="$emptySteps"/>
                        <xsl:with-param name="Value" select="Column[name='VALUE']/value"/>
                        <xsl:with-param name="Date" select="$sec + $fromSec + position() * $RI"/>
                        <xsl:with-param name="startDate" select="$sec + $fromSec + position() * $RI"/>
                    </xsl:call-template>
                    <xsl:call-template name="basic">
                        <xsl:with-param name="pos" select="$pos + 1"/>
                        <xsl:with-param name="sec" select="$sec + $emptySteps * $RI - $RI"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <!-- ======================================================== -->
    <xsl:template name="ManyDots">
        <xsl:param name="start"/>
        <xsl:param name="pos" select="1"/>
        <xsl:param name="Count" select="0"/>
        <xsl:param name="Sum" select="0"/>
        <xsl:variable name="startSec">
            <xsl:call-template name="Date2Sec">
                <xsl:with-param name="date" select="$start"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:for-each select="/All-list/DynReport-list/DynReport/ResultSet/Row/Columns">
            <xsl:sort select="Column[name='DT']/value"/>
            <xsl:if test="position() = $pos">
                <xsl:variable name="curSec">
                    <xsl:call-template name="Date2Sec">
                        <xsl:with-param name="date" select="Column[name='DT']/value"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="$curSec &gt;= $start and ($start + $RI) &gt;= $curSec">
                        <xsl:call-template name="ManyDots">
                            <xsl:with-param name="start" select="$start"/>
                            <xsl:with-param name="pos" select="position() + 1"/>
                            <xsl:with-param name="Count" select="$Count + 1"/>
                            <xsl:with-param name="Sum" select="$Sum + Column[name='VALUE']/value"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="($curSec &gt; ($start + $RI) or position()=last()) and $Count != 0">
                        <xsl:value-of select="$Sum div $Count"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="ManyDots">
                            <xsl:with-param name="start" select="$start"/>
                            <xsl:with-param name="pos" select="position() + 1"/>
                            <xsl:with-param name="Count" select="$Count"/>
                            <xsl:with-param name="Sum" select="$Sum"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <!-- ======================================================== -->
    <xsl:template name="Date2Sec">
        <xsl:param name="date"/>
        <xsl:variable name="Time" select="substring-after($date, ' ')"/>
        <xsl:variable name="Sec" select="substring-before($Time, ':') * 24 * 60 + substring($Time, 7, 2) + substring($Time, 4, 2) * 60"/>
        <xsl:value-of select="$Sec"/>
    </xsl:template>
    <!-- ======================================================== -->
    <xsl:template name="for">
        <xsl:param name="i" select="0"/>
        <xsl:param name="n"/>
        <xsl:param name="Value"/>
        <xsl:param name="Date"/>
        <xsl:param name="startDate"/>
        <xsl:if test="$i &lt; $n">
            <xsl:value-of select="concat($Value, ',', $Date, ',')"/>
            <xsl:call-template name="for">
                <xsl:with-param name="i" select="$i + 1"/>
                <xsl:with-param name="n" select="$n"/>
                <xsl:with-param name="Value" select="$Value"/>
                <xsl:with-param name="Date" select="$startDate + $i * $RI + $RI"/>
                <xsl:with-param name="startDate" select="$startDate"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <!-- ======================================================== -->
    <xsl:template name="next">
        <xsl:param name="pos"/>
        <xsl:for-each select="/All-list/DynReport-list/DynReport/ResultSet/Row/Columns">
            <xsl:sort select="Column[name='DT']/value"/>
            <xsl:if test="position()=($pos + 1)">
                <xsl:call-template name="Date2Sec">
                    <xsl:with-param name="date" select="Column[name='DT']/value"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>