Karthikeyan
Karthikeyan

Reputation: 2001

xml, xslt - header and footer on all pages with continuing table data contents

I have a requirement to generate PDF file from XML document. And that PDF should have header and footer on each and every page, and in the body of the PDF should have table data from XML which should span across pages with header and footer

for example :

Page 1

Header Contents :

Table :
Line 1
Line 2

Footer Contents :

Page 2

Header Contents :

Table :
Line 3
Line 4

Footer Contents :

Its like, i should display limited rows in a table per page. only 2 rows in a table per page remaining next page with some header and footer contents.

Please find my XML :

<receipt>
    <order>
        <page></page>
        <page>
            <line_number>1</line_number>
            <product_code>S10</product_code>
            <line_number>2</line_number>
            <product_code>S20</product_code>
        </page>
        <page>
            <line_number>3</line_number>
            <product_code>S92</product_code>
            <line_number>4</line_number>
            <product_code>S31</product_code>
        </page>
    </order>
</receipt>

Please find my xsl stylesheet :

xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">

    <xsl:template match="/receipt">
        <html>
            <head>
            <style>@page {size: a4 landscape;}</style>
            </head>
            <body>

                <table >
                    <thead>
                        <tr >
                            <th >Line</th>
                            <th>Item Code</th>
                        </tr>
                    </thead>
                <tbody>
                  <xsl:for-each select="order/page/line_number">
                            <tr style="font-size: 9px; ">
                                <td><xsl:value-of select="." /></td>
                                <td><xsl:value-of select="following-sibling::product_code[1]" /></td>
                            </tr>
                 </xsl:for-each>
                </tbody>
                </table>
                <br />

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 1786

Answers (1)

Toshihiko Makita
Toshihiko Makita

Reputation: 1304

Try following code. Adding page-break-before:always; style for the first generation of tr element will split table by <page> element. This will solve your requirement.

<tbody>
    <xsl:for-each select="order/page[exists(*)]">
        <xsl:for-each-group select="*" group-adjacent="count(self::line_number|preceding-sibling::line_number)">
            <xsl:variable name="pos" as="xs:integer" select="position()"/>
            <tr style="{concat('font-size: 9px;',if ($pos eq 1) then 'page-break-before:always;' else '')}">
                <td>
                    <xsl:value-of select="current-group()[1]"/>
                </td>
                <td>
                    <xsl:value-of select="current-group()[2]"/>
                </td>
            </tr>
        </xsl:for-each-group>
    </xsl:for-each>
</tbody>

Upvotes: 2

Related Questions