Solved

Keeping count/position of table rows

Posted on 2001-07-14
6
301 Views
Last Modified: 2008-02-26
Hi,
Below is a piece of an xslt file, a single template for a table that will extend over many pages.  I also have some fixed items on the pages (not included here) so this table can only render x number of rows per page, then there will be a page break and the table will continue on the next page.  I'm not sure how to keep track of the  rows so that say 12 rows will print on page one, then page two will begin with row 13 and print another 12 rows, etc.  Do I use position(), do I need a param?  Any direction, would be appreciated. Thanks!


<xsl:template name="pagetable">
<table id="table1">
<caption><xsl:value-of select="/statement/accountInfo/acctnum"/>&#xa0;<!--whitespace between elements--><xsl:value-of select="/statement/accountInfo/accttitle"/></caption>

<caption id="caption2">
<xsl:value-of select="/statement/text"/>&#xa0;<xsl:value-of select="format-number(transactions/@beginBal,'#,##0.00DB;#,##0.00CR')"/></caption>

<xsl:for-each select="transactions/transaction">

<tr id="tr1">

<td width="65px" align="left"><xsl:value-of select="date"/>&#xa0;</td>
<td width="70px"><xsl:value-of select="long"/>&#xa0;</td>
<td width="70px"><xsl:value-of select="short"/>&#xa0;</td>
<td width="225px"><xsl:value-of select="description"/>&#xa0;</td>
<td width="50px" align="center"><xsl:value-of select="priceCode"/>&#xa0;</td>

<td width="160px" align="right">
<xsl:if test="substring(amount,1,1)!='-'">
<xsl:value-of select="format-number(amount,'#,##0.00')"/>&#xa0;</xsl:if></td>

<td width="160px" align="right">
<xsl:if test="substring(amount,1,1)='-'">
<xsl:value-of select="format-number(amount,'#,##0.00')"/>&#xa0;</xsl:if></td>

<td width="180px" align="right">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:value-of select="format-number(amount+//@beginBal,'#,##0.00DB;#,##0.00CR')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(amount+sum(preceding-sibling::transaction/amount)+//@beginBal,'#,##0.00DB;#,##0.00CR')"/>&#xa0;
</xsl:otherwise>
</xsl:choose>

</td>
</tr>
</xsl:for-each> <!--transactions/transaction-->
</table>
</xsl:template>
0
Comment
Question by:Tamre
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:sdussinger
ID: 6284206
Let's see if I undertand your question. You have an XML file which you will run this transform over several times to display all of the rows in the XML file. For example, if you can display 12 rows at a time and your XML file contains 60 rows, you'll apply this template 5 times to produce 5 pages, each containing 12 rows. Is that a fair assessment?

If so, you probably need to do something like the following:

1) Pass two params to your XSLT. The starting index for this page, and the number of entries to process.

2) Use the position() operation in the body of your for-each loop to determine if the current position() is between the starting index and starting index + num entries.  If the index is within that range, then output the rows as you do in your current XSLT. Otherwise do nothing.

Hopefully, this description is clear enough to get you going. If not, let me know and I'll try to give you something more specific.

On the other hand, if my assessment of what you're trying to do is off-base, post some more details, and I'll try again.

--Steve
0
 

Author Comment

by:Tamre
ID: 6287741
Steve--an excellent assessment.  I have been working on this, a step at a time. I've tried to put the recursion in to print ten rows...although it is not yet working.
Do you see anything that jumps out and screams error?  I'm not real coiomfortable with recusion yet--I program in C--this is a little new.  Anyway, I'm not sure if I am WAY off, or just a bit. It would help knowing. It seems like after row 10 it would stop, but it is printing all the rows in the xml file at this point.

You mentioned position(), also.  Would that be used to track the tables rows from page to page--or for anyother purpose?  Somehow it does need to know that it should print rows 1-10 on page one, and must begin with row 11 on page two. I have no way of knowing the total rows (transactions) in advance as it will vary.

I indented my param stuff.  Hopefully it will render that way.

Thanks for your help....Tamre


<xsl:template name="pagetable">
<table id="table1">
<caption><xsl:value-of select="/statement/accountInfo/acctnum"/>&#xa0;<!--whitespace between elements--><xsl:value-of select="/statement/accountInfo/accttitle"/></caption>

<caption id="caption2">
<xsl:value-of select="/statement/text"/>&#xa0;<xsl:value-of select="format-number(transactions/@beginBal,'#,##0.00DB;#,##0.00CR')"/></caption>


    <xsl:for-each select="transactions/transaction">

    <xsl:call-template name="rowcount">
    <xsl:with-param name="start-val" select="1"/>
    <xsl:with-param name="end-val" select="10"/>
    </xsl:call-template>


</xsl:for-each> <!--transactions/transaction-->
</table>
</xsl:template>

    <xsl:template name="rowcount">
    <xsl:param name="start-val" select="0"/>
    <xsl:param name="end-val" select="0"/>
    <xsl:if test="$start-val &lt;= $end-val">

<tr id="tr1">

<td width="65px" align="left"><xsl:value-of select="date"/>&#xa0;</td>
<td width="70px"><xsl:value-of select="long"/>&#xa0;</td>
<td width="70px"><xsl:value-of select="short"/>&#xa0;</td>
<td width="225px"><xsl:value-of select="description"/>&#xa0;</td>
<td width="50px" align="center"><xsl:value-of select="priceCode"/>&#xa0;</td>


<td width="160px" align="right">
<xsl:if test="substring(amount,1,1)!='-'">
<xsl:value-of select="format-number(amount,'#,##0.00')"/>&#xa0;</xsl:if></td>

<td width="160px" align="right">
<xsl:if test="substring(amount,1,1)='-'">
<xsl:value-of select="format-number(amount,'#,##0.00')"/>&#xa0;</xsl:if></td>


<td width="180px" align="right">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:value-of select="format-number(amount+//@beginBal,'#,##0.00DB;#,##0.00CR')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(amount+sum(preceding-sibling::transaction/amount)+//@beginBal,'#,##0.00DB;#,##0.00CR')"/>&#xa0;
</xsl:otherwise>
</xsl:choose>

</td>
</tr>

    <xsl:call-template name="rowcount">
    <xsl:with-param name="start-val" select="$start-val + 1"/>
    <xsl:with-param name="end-val"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
0
 
LVL 4

Accepted Solution

by:
sdussinger earned 150 total points
ID: 6287934
OK. I'm kinda winging it here, since I don't have any way to run your stuff on my machine. But I noticed something which looks a little odd.  You mentioned using recursion to do your processing, and I noticed that you're calling the row-count template from within the row-count template.

My question is why?

You have a for-each loop running over all of the elements in sequence. Your row-count template should just determine if the element in question should be printed or not, print it if it should be, and then return. There should be no need to recurse, unless I misunderstood something in your earlier post.

I would try something like:

In the for-each loop, compare the position () of each element to the start and end indices of the items you want to display. If the position () is in the correct range, call the row-count template, passing the current transaction, and let the row-count template just process the current element. Remove all of the if-then stuff from row-count that has to do with determining if the element is in the correct range, and remove the recursive call back to row-count.

This should do what you want.  If it's not clear, send me the entire XSLT and an XML sample, and I'll work on it some more.

--Steve
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Tamre
ID: 6288468
Steve--

Good question!!  Your help has been great.  I have this piece up and running.  Thanks to your keen eye and great comments.

As I continue along this project, I may have another question or two...I hope you'll be around.

Thanks again,  Tamre
0
 

Author Comment

by:Tamre
ID: 6288471
Steve was very helpful.  Sometimes you get stuck on viewing something in one way and it is nice to have another pair of eyes to help you see it in another light.
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6291122
Glad I could help. If you run into another question, post it here, and I'll help if I can.

--Steve
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question