Anatomy of a Template I - Fixed Row Enumeration
The bursting articles are still in process ... I did some work a while back for a training course on the 'anatomy of an invoice template' ... there are some useful 'bits' that I thought you might like to see.
One of the most requested features on the forum for RTF templates is to show a fixed number of rows per page. Maybe you have pre-printed stationary that can only take a certain number of lines, maybe you have a functional requirement for it. For whatever the reason id the functional folks have given it to you to implement. Up until now there has been a template floating around that I think I let loose that shows how this can be done for invoices. There is little explanation of whats going on and how it's done. I'll try and make ammends to those of you that may have gotten a little lost but plugged it in anyway and it worked so what the heck.
I have started off small and we'll build this template up into a full invoice format that can run against the standard AR Oracle Report in 11i (RAXINV) ... those of you that are not EBSer's or are not interested in the invoice format, dont worry, you can follow along and apply the same principles to any data set. Here's the features out template is going to have.
1. A fixed number of rows per page,
2. 'Filler' space for lines, because that last page may only have 3 lines but you want to maintain the layout
3. Page totals or 'Continued' in place of a total
4. Last page only content
5. Header and page number resetting as we hit each new invoice in the batch.
For all of the explanations we are going to use the attached XML. Its a full invoice batch, thats multiple invoices having multiple lines.
The Data
Lets start with number 1, for the fixed row enumeration we are only interested in the lines section of the XML, so we can focus on this portion Im going to ignore the rest of the XML. We are only interested in the G_LINES group of the XML structure:
<G_LINES>
<LINE_NUMBER>1</LINE_NUMBER>
<LINE_CUSTOMER_TRX_ID>1903</LINE_CUSTOMER_TRX_ID>
<LINE_CUSTOMER_TRX_LINE_ID>1801</LINE_CUSTOMER_TRX_LINE_ID>
<LINE_CHILD_INDICATOR>0</LINE_CHILD_INDICATOR>
<LINE_TYPE>LINE>LINE</LINE_TYPE>
...
</G_LINES>
<G_LINES>
<LINE_NUMBER>2</LINE_NUMBER>
<LINE_CUSTOMER_TRX_ID>1903</LINE_CUSTOMER_TRX_ID>
<LINE_CUSTOMER_TRX_LINE_ID>1817</LINE_CUSTOMER_TRX_LINE_ID>
<LINE_CHILD_INDICATOR>1</LINE_CHILD_INDICATOR>
<LINE_TYPE>LINE>LINE</LINE_TYPE>
...
</G_LINES>
There are obviously a lot more elements and a lot more lines but all we are interested in is, the grouping and the line type for now.
The Template
Now take a look at the template; if you just look at the table and nothing else it all looks pretty normal. We have a for-each, some fields followed by an end for-each ... straightforward stuff right. You'll notice the for-each has a little more going on thou and there's that 'if' condition:
<?for-each:$invLines?>
<?if:position()>=$start and position()<$start+$lpp?>
I'll come back to these, lets look first at the fields above the table:
LinesPerPageVariable - this does what it says on the can and sets up the number of lines we want to see per page and assigns it to a variable 'lpp'. Now remember native XSL 'variables' are not like other language variables, think of them more as constants.
<xsl:variable name="lpp" select="number(15)"/>
LinesTreeVariable - this holds not just a single value but a complete tree of values. Word of caution here, if you are going to do this you need to be aware that the XSLT engine is going to load this tree into memory ... so do not load huge trees. In our completed invoice template we will only load the lines tree for each invoice. In this example we are loading all the lines in the XML regardless of invoice ... thats OK thou, our XML is small, just be aware of what are doing here.
We have a variable 'invLines' and we are loading the members of the G_LINES group where the TYPE is equal to 'LINE'. Notice we use an XPATH expression to do this. We also use the 'incontext' command to ensure we are picking up the lines only for the current position i.e. within the current invoice. For this example remember we have no invoice header so we pick up all lines into the tree.
<xsl:variable xdofo:ctx="incontext" name="invLines" select=".//G_LINES[LINE_TYPE='LINE']"/>
FEinvLines - this is the first loop we need to to go over the G_LINES group we created earlier. Although there is only a single group we need to iterate over it so we can set up a variable to hold the starting line position.
The if statement is checking if the record position we have reached modulizing with the line per page count equals zero i.e. we have reached the first record the create a variable (constant) called 'start' and initialize it with '0'.
<?for-each:$invLines?>
<?if:(position()-1) mod $lpp=0?>
<xsl:variable name="start" xdofo:ctx="incontext" select="position()"/>
Now we get into the table, the first field in their is another for-each but this time we are going to loop over the members of the invLines tree. Notice the use of the '$' to reference the variable.
<?for-each:$invLines?>
<?if:position()>=$start and position()<$start+$lpp?>
The if statement here is checking that the current record pointer 'position()' is either greater than 'start' ie the first record or less the 'lpp' value we set up earlier. If it is then show the record otherwise not. the rest of the table is standard stuff.
Now we wanted a fixed number of rows per page and the logic above will provide that but of course the template needs to signify the need for a page break after the alloted number of rows have been shown. Looking at the 'Page Break' field:
<xsl:if xdofo:ctx="inblock" test="$start+$lpp<=count($group)">
<xsl:attribute name="break-before">page</xsl:attribute>
</xsl:if>
We have an 'if' statement wrapped around the page break instruction. This is just like the if in the table, if the specified number of rows has been met then insert a page break.
So thats fixed row enumeration, hope things are a little clearer. Next we'll add in the header for th invoices so we can then ensure we have a fixed layout per page of each invoice. Complete template, data and PDF are here.