Link to home
Start Free TrialLog in
Avatar of vishey68
vishey68

asked on

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

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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
like this
<xsl:if test="last() &lt; 5">...

Open in new window

Avatar of vishey68
vishey68

ASKER

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>
My bad the above will not work. I need something which will check only when the last row is being displayed
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

Make sure you close the <tr? tags appropriately before getting to the <xsl:if>.
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()">
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
take your time, I don't leave :)
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
<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
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

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