<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
   <xsl:param name="fixStartEnd">18:19</xsl:param>
   

<xsl:variable name = "fixStart" select = "number(substring-before($fixStartEnd,':'))" />   
<xsl:variable name = "fixEnd" select = "number(substring-after($fixStartEnd,':'))" />   

   <xsl:template match="/">
      <data>
         <FIXDATES>
            <xsl:apply-templates select="//data/FIXDATES/FD" />
         </FIXDATES>

         <xsl:apply-templates select="//data/data" />
      </data>
   </xsl:template>

   <xsl:template match="FD">
      <xsl:if test="day[1]/@od &lt;= $fixStart">
         <xsl:copy-of select="." />
      </xsl:if>
   </xsl:template>

   <xsl:template match="data">
      <data id="{@id}" lvl="{@lvl}" nm="{@nm}" sht="{@sht}">
         <xsl:apply-templates select="ctry" />
      </data>
   </xsl:template>

   <xsl:template match="ctry">
      <ctry id="{@id}" lvl="{@lvl}" nm="{@nm}" sht="{@sht}">
         <xsl:apply-templates select="dvn" />
      </ctry>
   </xsl:template>

   <xsl:template match="dvn">
      <xsl:variable name="newFix" select="TM/R[@v = 'H' and @d &gt;= $fixStart and @d &lt;= $fixEnd]" />

      <dvn id="{@id}" lvl="{@lvl}" nm="{@nm}" sht="{@sht}">
         <FIXLST>
            <xsl:apply-templates select="$newFix" mode="Result-to-Fixture" />
         </FIXLST>

         <xsl:apply-templates select="TM" />
      </dvn>
   </xsl:template>

   <xsl:template match="TM">
      <xsl:variable name="fResults" select="R[@d &lt; $fixStart]" />

      <TM id="{@id}" nm="{@nm}">
         <xsl:copy-of select="$fResults" />
      </TM>
   </xsl:template>

   <xsl:template match="R" mode="Result-to-Fixture">
      <R h="{parent::node()/@id}" a="{@o}" d="{@d}" f="{@f}" g="{@g}" r="{@r}" />
   </xsl:template>
</xsl:stylesheet>


