Marco
Marco

Reputation: 11

Can I create a cycle that increment number of pages?

I have to create a cycle that increment a number previously set. This is the situation:

  1. I receive a xml tag (es. pagnum) with a "start-number" of pages (es. like 1120)
  2. I need to increment this number according with the number of printed pages(es. like print 60 pages?then 1120-1121-1122 ... 1280)

I imagine a cycle like : pagnum+1 --> increment pagnum of 1 unit and go on until last page. I have no idea to do this (like in C++) on xsl language.

I tried this but doesn't work:

<xsl:template match="documento">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
    <fo:simple-page-master
            master-name="pages-normale"
            page-width="297mm"
            page-height="210mm"
            margin-top="0.5cm"
            margin-bottom="0.5cm"
            margin-left="0.5cm"
            margin-right="0.5cm">
        <fo:region-body   margin-top="2cm" extent="20cm"/>
        <fo:region-before margin-top="1cm" margin-bottom="1cm" extent="7cm"/>
        <fo:region-after  margin-top="1cm" margin-bottom="1cm" extent="1cm"/>
    </fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence
        master-reference="pages-normale"
        initial-page-number="1"
        force-page-count="even"
        language="it"
        country="it">
        <fo:static-content flow-name="xsl-region-before">
        <fo:block>
        <fo:table  width="100%" border-width="0.5mm" margin-bottom="4mm" border-style="transparent">
        <fo:table-column  column-width="50%"/>
        <fo:table-column  column-width="50%"/>
        <fo:table-header>
                <fo:table-row>
                                <fo:table-cell border-style="normal">
                                    <fo:block font-size="15" font-style="oblique" font-weight="bold">
                                    Scipola S.r.l           
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell border-style="normal">
                                    <fo:block font-size="8" font-weight="normal" text-align="right">
                                    <xsl:template name="recursive-template">
                                       <xsl:param name="var" select="pagnum"/>
                                       <xsl:choose>
                                         <xsl:when test="$var > 0">
                                           <xsl:value-of select="$var"/>
                                           <xsl:call-template name="recursive-template">
                                             <xsl:with-param name="var" select="$var + 1"/>
                                           </xsl:call-template>
                                         </xsl:when>
                                         <xsl:otherwise/>
                                       </xsl:choose>
                                    </xsl:template>
                                    </fo:block>
                                </fo:table-cell>
                                </fo:table-row>
                </fo:table-header>

Any suggestion?

I expect that the number page, shown on the right-top of the paper, start from 1120 and increment with every printed pages.

Upvotes: 0

Views: 236

Answers (3)

Kevin Brown
Kevin Brown

Reputation: 8877

Because you are using XSL FO, if your intention is to use this as the page numbering in the document for the printed pages, then you do not need anything like you are trying to do.

You see in your template, you have initial-page-number="1" on the <fo:page-sequence>. Just set that to 1120 and not 1 and let the formatter do its job and apply page numbers. You use <fo:page-number/> to output the page number.

Now if you are looking for a solution where you can process this information in sequence, then FOP provides a log or likely API call or logger in which you can examine the results of the format and get the number of pages that resulted. You could use that number as input to the next cycle. I would use an <xsl:param> passed into the XSLT to set the next initial-page-number.

Without deeper coding into the formatter which it is running in the XSL FO to output stage, you cannot use the page-number in anyway and certainly not in the XSLT stage because unless you are controlling the whole pagination (meaning you are injecting page breaks and you are guaranteeing that content between them is shorter than a page), you have no idea what that pagination will be at XSLT.

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116992

Consider the following simplified example:

XML

<input>
    <start>1120</start>
    <amount>20</amount>
</input>

XSLT 2.0

<xsl:stylesheet version="2.0"   
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/input">
    <output>
        <xsl:for-each select="start to xs:integer(start + amount - 1)">
            <page n="{position()}">
                <xsl:value-of select="."/>
            </page>
    </xsl:for-each>
    </output>
 </xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <page n="1">1120</page>
   <page n="2">1121</page>
   <page n="3">1122</page>
   <page n="4">1123</page>
   <page n="5">1124</page>
   <page n="6">1125</page>
   <page n="7">1126</page>
   <page n="8">1127</page>
   <page n="9">1128</page>
   <page n="10">1129</page>
   <page n="11">1130</page>
   <page n="12">1131</page>
   <page n="13">1132</page>
   <page n="14">1133</page>
   <page n="15">1134</page>
   <page n="16">1135</page>
   <page n="17">1136</page>
   <page n="18">1137</page>
   <page n="19">1138</page>
   <page n="20">1139</page>
</output>

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163322

There are several things wrong with your code. Most obviously, xsl:template can only appear as a top-level element, that is, an immediate child of xsl:stylesheet: it can't be nested inside another xsl:template. The other thing that's clearly wrong is that your recursive template doesn't terminate: if called with a parameter value > 0 then it will continue calling itself indefinitely.

Apart from those details, you've roughly got the right idea - though I haven't examined your problem in enough detail to see if there might be a much simpler way of doing it.

Upvotes: 1

Related Questions