home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F46608_xsPartialSort.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-07-20  |  5.3 KB  |  164 lines

  1. <xsl:stylesheet version="1.0"
  2.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. >
  4.  
  5.     <xsl:output method="text"/>
  6.  
  7.  <xsl:variable name="theBuffer2">
  8.    <xsl:for-each select="/dates/datesUnSorted/date[position() < 6]">
  9.     <xsl:sort select="@value"/>
  10.        <xsl:value-of select="concat(@value, ' ')"/>
  11.    </xsl:for-each>
  12.   </xsl:variable>
  13.  
  14.  
  15.  
  16.  <xsl:template match="/">
  17.  
  18.    <xsl:variable name="theResult">
  19.      <xsl:call-template name="mergeSingleNode">
  20.       <xsl:with-param name="theSiblings" select="/dates/datesUnSorted/date"/>
  21.       <xsl:with-param name="siblPosition" select="6"/>
  22.       <xsl:with-param name="sortPad" select="$theBuffer2"/>
  23.      </xsl:call-template>
  24.    </xsl:variable>
  25.  
  26.    <xsl:value-of select="$theResult"/>
  27.  </xsl:template>
  28.  
  29.    <xsl:template name="mergeSingleNode">
  30.        <xsl:param name="theSiblings" select="/.."/>
  31.     <xsl:param name="siblPosition" select="0"/>
  32.     <xsl:param name="sortPad" />
  33.  
  34.     <xsl:choose>
  35.      <xsl:when test="$siblPosition > count($theSiblings)">
  36.  
  37.       <xsl:value-of select="concat('Result: ', $sortPad)"/>
  38.      </xsl:when>
  39.      <xsl:otherwise>
  40.         <xsl:variable name="Last"
  41.                           select="string-length($sortPad) div 9"/>
  42.           <xsl:variable name="argNumber"
  43.                       select="$theSiblings[$siblPosition]/@value"/>
  44.  
  45.           <xsl:variable name="mergePosition">
  46.           <xsl:call-template name="binSearch">
  47.                <xsl:with-param name="argNumber" select="$argNumber"/>
  48.                <xsl:with-param name="sortedEls" select="$sortPad"/>
  49.                <xsl:with-param name="First" select="1"/>
  50.                <xsl:with-param name="Last" select="$Last"/>
  51.           </xsl:call-template>
  52.       </xsl:variable>
  53.  
  54.       <xsl:choose>
  55.              <xsl:when test="$mergePosition > 0">
  56.  
  57.               <xsl:variable name="newSortPad">
  58.                 <xsl:if test="$mergePosition > 1">
  59.                <xsl:value-of select="substring($sortPad,
  60.                                             10,
  61.                                             ($mergePosition - 1) * 9)"/>
  62.                 </xsl:if>
  63.  
  64.                 <xsl:value-of select="concat($argNumber, ' ')"/>
  65.  
  66.                 <xsl:if test="$mergePosition != $Last">
  67.                <xsl:value-of
  68.                 select="substring($sortPad,
  69.                             $mergePosition * 9 + 1,
  70.                                         9 * ($Last - $mergePosition)) "/>
  71.                 </xsl:if>
  72.               </xsl:variable>
  73.  
  74.  
  75.               <!-- Now the recursive call -->
  76.  
  77.              <xsl:call-template name="mergeSingleNode">
  78.                 <xsl:with-param name="theSiblings" select="$theSiblings"/>
  79.                 <xsl:with-param name="siblPosition"                                select="$siblPosition + 1"/>
  80.                 <xsl:with-param name="sortPad" select="$newSortPad"/>
  81.              </xsl:call-template>
  82.              </xsl:when>
  83.              <xsl:otherwise>
  84.  
  85.                 <!-- A recursive call with the same $sortPad-->
  86.  
  87.              <xsl:call-template name="mergeSingleNode">
  88.                 <xsl:with-param name="theSiblings" select="$theSiblings"/>
  89.                 <xsl:with-param name="siblPosition"
  90.                             select="$siblPosition + 1"/>
  91.                 <xsl:with-param name="sortPad" select="$sortPad"/>
  92.              </xsl:call-template>
  93.              </xsl:otherwise>
  94.       </xsl:choose>
  95.     </xsl:otherwise>
  96.    </xsl:choose>
  97.  
  98.    </xsl:template>
  99.  
  100.  <xsl:template name="binSearch">
  101.   <xsl:param name="argNumber" select="-Infinity"/>
  102.   <xsl:param name="sortedEls" />
  103.   <xsl:param name="First" select="Infinity"/>
  104.   <xsl:param name="Last" select="0"/>
  105.  
  106.   <xsl:choose>
  107.    <xsl:when test="$First > $Last">
  108.        <xsl:value-of select="$Last"/>
  109.    </xsl:when>
  110.    <xsl:otherwise>
  111.        <xsl:variable name="Mid"
  112.                   select="floor(($First + $Last) div 2)"/>
  113.         <xsl:variable name="midElement"
  114.                        select="substring($sortedEls,
  115.                                       9 * ($Mid - 1) + 1,
  116.                                       8)"/>
  117.  
  118.      <xsl:choose>
  119.         <xsl:when test="$argNumber = $midElement">
  120.          <xsl:value-of select="$Mid"/>
  121.         </xsl:when>
  122.       <xsl:when test="$argNumber < $midElement">
  123.        <xsl:choose>
  124.         <xsl:when test="$First < $Last">
  125.           <xsl:call-template name="binSearch">
  126.            <xsl:with-param name="argNumber" select="$argNumber"/>
  127.            <xsl:with-param name="sortedEls" select="$sortedEls"/>
  128.            <xsl:with-param name="First" select="$First"/>
  129.            <xsl:with-param name="Last" select="$Mid - 1"/>
  130.           </xsl:call-template>
  131.        </xsl:when>
  132.        <xsl:otherwise><!-- First = Last and the comparison is lt -->
  133.          <xsl:value-of select="$First - 1"/>
  134.        </xsl:otherwise>
  135.       </xsl:choose>
  136.      </xsl:when>
  137.      <xsl:when test="$argNumber > $midElement">
  138.         <xsl:choose>
  139.           <xsl:when test="$First < $Last">
  140.            <xsl:call-template name="binSearch">
  141.              <xsl:with-param name="argNumber" select="$argNumber"/>
  142.              <xsl:with-param name="sortedEls" select="$sortedEls"/>
  143.              <xsl:with-param name="First" select="$Mid + 1"/>
  144.              <xsl:with-param name="Last" select="$Last"/>
  145.              </xsl:call-template>
  146.            </xsl:when>
  147.           <xsl:otherwise><!-- First = Last and the comparison is gt -->
  148.            <xsl:value-of select="$Last"/>
  149.           </xsl:otherwise>
  150.         </xsl:choose>
  151.      </xsl:when>
  152.      <xsl:otherwise>
  153.         ERRROR
  154.      </xsl:otherwise>
  155.  
  156.    </xsl:choose>
  157.   </xsl:otherwise>
  158.  
  159.  
  160.  </xsl:choose>
  161. </xsl:template>
  162.  
  163. </xsl:stylesheet>
  164.