Row number of the last row being processed

I need to count the number of rows being processed when displaying in XSLT and if the count of rows are less tha 5 i have to add the differnce of 5 and the number of rows. For eg if the total count is 2 then 5-2 = 3 i have to add 3 empty rows to the present code shown below.
<xsl:for-each select="Accounts/Account[WELLS_FARG_ACCT_TYPE_CD='C']">
                                <tr>   
				 <xsl:choose>
				 <xsl:when test="position() mod 2 = 0">
                                 <xsl:attribute name="bgcolor">
                                 <xsl:text>#ff00ff</xsl:text>
                                 </xsl:attribute>
                                 </xsl:when>
				 <xsl:otherwise>
                                 <xsl:attribute name="bgcolor">
                                 <xsl:text>#5533ff</xsl:text>
                                 </xsl:attribute>
                                </xsl:otherwise>
                                 </xsl:choose>
                                    <td>
                                     <xsl:if test="position() = 1">
                                        <input type="radio" name="group3" checked="checked" onclick="clickRadio()"></input>
                                     </xsl:if>
                                     <xsl:if test="position() != 1">
                                        <input type="radio" name="group3" onclick="clickRadio()"></input>
                                     </xsl:if>
                                    </td>                         
                                    <td><xsl:value-of select="WELLS_ID"/></td>
                                    <td><xsl:value-of select="HI_LVL_PRNT_ID"/></td>
                                    <td><xsl:value-of select="CORP_TOT_PROG_CANC_DT"/></td>
                                    <td><xsl:value-of select="CORP_NM"/></td>
                                    <td><xsl:value-of select="CORP_DEROG_TOT_BAL_AMT"/></td>
                                </tr>

Open in new window

vishey68Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
The function last() will give you the number of rows iterated at any point

this xml
<test>
    <t>a</t>
    <t>b</t>
    <t>c</t>
    <t>d</t>
</test>

and this xsl

<xsl:template match="/">
    <xsl:for-each select="test/t">
        <xsl:value-of select="last()"/>
    </xsl:for-each>
</xsl:template>

will render 4444

you can use last() in a test to see if it is larger than five or not
0
Geert BormansInformation ArchitectCommented:
like this
<xsl:if test="last() &lt; 5">...

Open in new window

0
vishey68Author Commented:
Hi ,

I added this code at the end of last td in the above code. Instead of adding one more blank row at the end it is increasing the space between previous rows

 <xsl:if test="last() < 5">
   <tr>
</tr>                            
</xsl:if>
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

vishey68Author Commented:
My bad the above will not work. I need something which will check only when the last row is being displayed
0
vishey68Author Commented:
I have added the following code, but no row is getting added. My req is that i create an empty row. but when i code value-of-select it creates a row with previous values , but my req is to create empty row. Is this possible
<td><xsl:value-of select="AMEX_ID"/></td>
                                    <td><xsl:value-of select="HI_LVL_PRNT_ID"/></td>
                                    <td><xsl:value-of select="CORP_TOT_PROG_CANC_DT"/></td>
                                    <td><xsl:value-of select="CORP_NM"/></td>
                                    <td><xsl:value-of select="CORP_DEROG_TOT_BAL_AMT"/></td>
				    
				  
				      <xsl:if test="last() &lt; 5">
				       <xsl:if test="position() = 4">
				             <tr>
					      <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
				             </tr>
					
				       </xsl:if>
				        </xsl:if> 
				   
				    
                                </tr>

Open in new window

0
yeleCommented:
Make sure you close the <tr? tags appropriately before getting to the <xsl:if>.
0
Geert BormansInformation ArchitectCommented:
Do you create one empty row?
Or do you need to create empty rows, until you have five of them?

The test to create one empty row would be
<xsl:if test="last() < 5 and position() = last()">
0
Geert BormansInformation ArchitectCommented:
If you want to fill up until you have 5 rows,
you need to use a recursive named template (your version of a for-loop in XSLT1)
   <xsl:template match="/">
    <xsl:for-each select="test/t">
        <tr>
            <td><xsl:value-of select="."/></td>
        </tr>
        <xsl:if test="position() = last() and last() &lt; 5">
            <xsl:call-template name="add-empty-rows">
                <xsl:with-param name="number" select="5 - last()"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
    <xsl:template name="add-empty-rows">
        <xsl:param name="number"/>
        <xsl:if test="$number &gt; 0">
            <xsl:call-template name="add-empty-row"/>
            <xsl:call-template name="add-empty-rows">
                <xsl:with-param name="number" select="$number - 1"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template name="add-empty-row">
        <tr>
            <td></td>
        </tr>
    </xsl:template>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vishey68Author Commented:
Comment:  Sir, I think this looks good and it will work, but i will try tomorrow and let you know. I am accepting the solution without trying but if i open a request please bear with me
0
Geert BormansInformation ArchitectCommented:
take your time, I don't leave :)
0
vishey68Author Commented:
I have a question, this will create up to 5 rows can,  i place them under the td statement as given by my example or do i have to place them at specific points
0
Geert BormansInformation ArchitectCommented:
<xsl:templates> can not be nested
they need to be put next to eachother

If you need only one row then you need to call the second named template.
There you have to fill in the correct number of <td>

If you need a minimum of 5 rows, you need to call the first named template
with a parameter, being the correct number of rows you want to add.
Calling teh template happens inside the template where you do the iteration,
so you can steal that part from my example

The named templates need to be copied outside this template,
anywhere in the stylesheet
0
vishey68Author Commented:
Hello Sir,

The solution works. However when i modify the code to add alternative colors it displayes the following errors.

Keyword xsl:with-param may not be used here.

 <xsl:if test="position() = last() and last() &lt; 5">
                                     <xsl:call-template name="add-empty-rows">
                                         <xsl:with-param name="number" select="5 - last()"></xsl:with-param>
					 <xsl:with-param name="line" select="position()"></xsl:with-param>
                                     </xsl:call-template>
                                 </xsl:if>
				
                            </xsl:for-each> 
 
<xsl:template name="add-empty-rows">
        <xsl:param name="number"/>
	<xsl:param name="line"/>
        <xsl:if test="$number &gt; 0">
            <xsl:call-template name="add-empty-row"/>
	       <xsl:with-param name="line1" select="$line"></xsl:with-param>
            <xsl:call-template name="add-empty-rows">
                <xsl:with-param name="number" select="$number - 1"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template name="add-empty-row">
        <xsl:param name="line1"/>
        <tr>
	    
            <xsl:choose>
						 <xsl:when test="position() mod 2 = 0">
							 <xsl:attribute name="bgcolor">
								<xsl:text>#ff00ff</xsl:text>
							 </xsl:attribute>
						 </xsl:when>
						 <xsl:otherwise>
							<xsl:attribute name="bgcolor">
								<xsl:text>#5533ff</xsl:text>
							</xsl:attribute>
						</xsl:otherwise>
					</xsl:choose>
	    <td height="20px"><img src="images/spacer.gif"/></td>
	    <td height="20px"><img SRC="images/spacer.gif"/></td>				         
	    <td height="20px"><img SRC="images/spacer.gif"/></td>				         
	    <td height="20px"><img SRC="images/spacer.gif"/></td>			
	    <td height="20px"><img src="images/spacer.gif"/></td>
	    <td height="20px"><img SRC="images/spacer.gif"/></td>
        </tr>
    </xsl:template> 
</xsl:stylesheet>

Open in new window

0
Geert BormansInformation ArchitectCommented:
There are two things
- there is a syntactical error: with-param needs to be inside the call template, not after it
<xsl:call-template name="add-empty-row"/>
as you see from the />, you closed it too soon
- there is a logical error, you can't rely on position() in the named template
but you made the right decission to pass the line number to the named template

I have made a stylesheet based on a test XML, I bet you can adapt it to your own needs
<xsl:template match="tests">
    <xsl:for-each select="test">
        <tr>
            <xsl:attribute name="bgcolor">
                <xsl:choose>
                    <xsl:when test="position() mod 2 = 0">
                        <xsl:text>#ff00ff</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>#5533ff</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <!-- put your cell logic here -->
        </tr>
        <xsl:if test="position() = last() and last() &lt; 5">
            <xsl:call-template name="add-empty-rows">
                <xsl:with-param name="number" select="5 - last()"></xsl:with-param>
                <xsl:with-param name="line" select="position() + 1"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each> 
</xsl:template>
 
    <xsl:template name="add-empty-rows">
        <xsl:param name="number"/>
        <xsl:param name="line"/>
        <xsl:if test="$number &gt; 0">
            <xsl:call-template name="add-empty-row">
                <xsl:with-param name="line1" select="$line"></xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="add-empty-rows">
                <xsl:with-param name="number" select="$number - 1"/>
                <xsl:with-param name="line" select="$line + 1"/>
             </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template name="add-empty-row">
        <xsl:param name="line1"/>
        <tr>
            <xsl:attribute name="bgcolor">
                <xsl:choose>
                    <xsl:when test="$line1 mod 2 = 0">
                        <xsl:text>#ff00ff</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>#5533ff</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <td height="20px"><img src="images/spacer.gif"/></td>
            <td height="20px"><img SRC="images/spacer.gif"/></td>				         
            <td height="20px"><img SRC="images/spacer.gif"/></td>				         
            <td height="20px"><img SRC="images/spacer.gif"/></td>			
            <td height="20px"><img src="images/spacer.gif"/></td>
            <td height="20px"><img SRC="images/spacer.gif"/></td>
        </tr>
    </xsl:template> 

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.