<?xml version='1.0' encoding='utf-8' ?><xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html"/>

	<xsl:include href="BinarySearch.xsl"/>	
	<xsl:include href="DisplayStringAsTable.xsl"/>	

 	<xsl:template match="/">

	<!-- 	
	The variable, "$SortedElements", will hold the resulting output from the
	call to mergeSingleNode(Our recurse sort template).	The contents of 
	$SortedElements can be described as a fixed-length string containing sorted date values.
	-->	
   <xsl:variable name="SortedElements">
     <xsl:call-template name="mergeSingleNode">
      <xsl:with-param name="theSiblings" select="/dates/datesUnSorted/date"/>
      <xsl:with-param name="siblPosition" select="2"/>
      <xsl:with-param name="sortPad" select="$theBuffer2"/>
     </xsl:call-template>
   </xsl:variable>


	<!-- The template, DisplayStringAsTable will display the sorted dates -->
	<xsl:call-template name="DisplayStringAsTable">
		<xsl:with-param name="strInput" select="$SortedElements"/>
		<xsl:with-param name="strColHeader" select="'Date'"/>
		<xsl:with-param name="strPageHeader" select="'The top 5 dates'"/>
	</xsl:call-template>
 </xsl:template>



	<!-- Create a buffer of the initial dates as a delimited string -->
	<xsl:variable name="lastDate" select="6"/>
	<xsl:variable name="delimiter" select="'|'"/>
	<xsl:variable name="theBuffer2">
		<xsl:for-each select="/dates/datesUnSorted/date[position() &lt; $lastDate]">
			<xsl:sort select="@value"/>
				<xsl:value-of select="concat($delimiter, @value)"/><br/>
			</xsl:for-each>
	</xsl:variable>



 <xsl:template name="mergeSingleNode">

   	<xsl:param name="theSiblings" select="/.."/>
    <xsl:param name="siblPosition" select="0"/>
    <xsl:param name="sortPad" />
	<xsl:variable name="sortLen" select= "9" />

    <xsl:choose>
     
	 <xsl:when test="$siblPosition > count($theSiblings)">
	 <!--   The when condition will be used to terminate this recursive call.	 
	 		The sorted order for the dates is set here and will be used as output for this template.
			This value will ultimately be used as the contents of the variable named "theResult"
			 -->
	  <xsl:value-of select="$sortPad"/>
     </xsl:when>
     
	 
	 <xsl:otherwise>
    	<xsl:variable name="Last" select="string-length($sortPad) div $sortLen"/>
    		<xsl:variable name="argNumber" select="$theSiblings[$siblPosition]/@value"/>

			<!-- Perform the initial sort-->
    		<xsl:variable name="mergePosition">
  				<xsl:call-template name="binSearch">
   					<xsl:with-param name="argNumber" select="$argNumber"/>
   					<xsl:with-param name="sortedEls" select="$sortPad"/>
   					<xsl:with-param name="First" select="1"/>
   					<xsl:with-param name="Last" select="$Last"/>
					<xsl:with-param name="SortLen" select="$sortLen"/>
  				</xsl:call-template>
			</xsl:variable>

			<xsl:choose>


     		<xsl:when test="$mergePosition > 0">


	      		<xsl:variable name="newSortPad">
					<!-- If the merge position is greater than 1 than return the left half of the result -->
	      			<xsl:if test="$mergePosition > 1">
	       				<xsl:value-of select="substring($sortPad, $sortLen + 1, ($mergePosition - 1) * $sortLen)"/>
	      			</xsl:if>
				
					<!-- Now, place insert the currently evaluated date -->
	      			<xsl:value-of select="concat('|', $argNumber)"/>

					<!-- Now, concatenate the right half of the dates -->
	      			<xsl:if test="$mergePosition != $Last">
	       				<xsl:value-of select="substring($sortPad, $mergePosition * $sortLen + 1, $sortLen * ($Last - $mergePosition)) "/>
	      			</xsl:if>
	      		</xsl:variable>


	      		<!-- Now the recursive call with the newly generated date -->

	   			<xsl:call-template name="mergeSingleNode">
	      			<xsl:with-param name="theSiblings" select="$theSiblings"/>
	      			<xsl:with-param name="siblPosition" select="$siblPosition + 1"/>
	      			<xsl:with-param name="sortPad" select="$newSortPad"/>
	     		</xsl:call-template>
	     		</xsl:when>
     		<xsl:otherwise>

      			<!-- A recursive call with the same date string-->

     			<xsl:call-template name="mergeSingleNode">
      				<xsl:with-param name="theSiblings" select="$theSiblings"/>
      				<xsl:with-param name="siblPosition" select="$siblPosition + 1"/>
      				<xsl:with-param name="sortPad" select="$sortPad"/>
     			</xsl:call-template>
     		</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
		</xsl:choose>

   </xsl:template>
 

</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c)1998-2001 eXcelon Corp.
<metaInformation>
<scenarios ><scenario name="TopDates" url="file://c:\XMLJournalQnA\Article2\version2\Q1\TopDates.xml" htmlbaseurl=""/></scenarios>
</metaInformation>
-->

