<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
   <xsl:param name="rows">15</xsl:param>
   <xsl:param name="cols">10</xsl:param>

   <xsl:variable name="COLS" select="//ROOT/COLS/COL" />

   <xsl:variable name="BALLS" select="//ROOT/BALLS/BALL" />

   <xsl:template match="/">
   	<xsl:variable name = "newBoard-Parse1">
            <xsl:call-template name="vert-Col-Cycle"/>
  	</xsl:variable>
      <ROOT>

         <BALLS>
    		<xsl:call-template name="empty-column">
			<xsl:with-param name="board" select="msxsl:node-set($newBoard-Parse1)/BALL"/>
		</xsl:call-template>
         </BALLS>

         <BEFORE>
            <xsl:copy-of select="//ROOT/BALLS" />
         </BEFORE>
      </ROOT>
   </xsl:template>


  <xsl:template name="empty-column">
       <xsl:param name="col" select="$cols - 1" />
        <xsl:param name="emptyCnt" select="0" />
      <xsl:param name="board" />
       
        <xsl:if test="$col &gt;= 0">
      		<xsl:variable name="col-BALLS" select="$board[@x = $col]" />
     
              	<xsl:apply-templates select = "$col-BALLS"  mode="empty-column">

					<xsl:with-param name="emptyCnt" select="$emptyCnt"/>
				</xsl:apply-templates>
          
          	<xsl:choose>
        		<xsl:when test="count($col-BALLS) = 0">        		
 		    		<xsl:call-template name="empty-column">
					<xsl:with-param name="col" select="$col - 1"/>
					<xsl:with-param name="emptyCnt" select="$emptyCnt + 1"/>
					<xsl:with-param name="board" select="$board"/>
				</xsl:call-template>
       		</xsl:when>
        	  
        		<xsl:otherwise>
 		    		<xsl:call-template name="empty-column">
					<xsl:with-param name="col" select="$col - 1"/>
					<xsl:with-param name="emptyCnt" select="$emptyCnt"/>
					<xsl:with-param name="board" select="$board"/>
				</xsl:call-template>
       		</xsl:otherwise>
        	</xsl:choose>
   
      	</xsl:if>

   </xsl:template>

    <xsl:template match="BALL" mode="empty-column">
     <xsl:param name="emptyCnt" />
 		
  		
 	      		<BALL y="{@y}" x="{@x + $emptyCnt}" id="{@id + $emptyCnt}" color="{@color}"/>
        	
 
    </xsl:template>




   <xsl:template name="vert-Col-Cycle">
      <xsl:param name="col" select="0" />

      <xsl:if test="$col &lt; $cols">
         <xsl:choose>
            <xsl:when test="$COLS[@x = $col]">
            	
            	<xsl:apply-templates select = "$BALLS[@x = $col]" >
            	    <xsl:sort select="@y" data-type="number" order="ascending" />

					<xsl:with-param name="colSelected" select="$COLS[@x = $col]/BALL"/>
				</xsl:apply-templates>
            </xsl:when>

            <xsl:otherwise>
               <xsl:copy-of select="$BALLS[@x = $col]" />
            </xsl:otherwise>
         </xsl:choose>
         
 		<xsl:call-template name="vert-Col-Cycle">
			<xsl:with-param name="col" select="$col + 1"/>
		</xsl:call-template>
        
         
      </xsl:if>
   </xsl:template>
   
    <xsl:template match="BALL">
  		
  		
 	<xsl:param name="colSelected"/>
   	<xsl:variable name="cid" select="@id" />
 	
   	<xsl:variable name="in-col" select="$colSelected[@id = $cid]" />
   	<xsl:variable name="below-me" select="$colSelected[@id &lt;= $cid]" />
    
    
    
    <xsl:choose>
    	<xsl:when test="not($below-me)">    	
    		<xsl:copy-of select = "."/>
    	</xsl:when>
    	<xsl:when test="not($in-col)"> 
    	      <BALL y="{@y  - count($below-me)}" x="{@x}" id="{@id - count($below-me) * $cols}" color="{@color}"/>
     	</xsl:when>
      
    </xsl:choose>
 
 
 
     
    </xsl:template>
 
   
</xsl:transform>


