Search code examples
xmlxsltxsl-fo

XSL-FO: How to set initial page number for whole document?


My xml is root with many different child element in any order, each with its own markup inside page-sequence. Xslt template takes one param - initial page of this document. And I cant set it in initial-page-number attribute for page-sequence because dont know which element will be first. Is there any workarounds in xsl-fo or xslt level for applying given number as page number of first page-sequence?

xml example 1:

<root>
 <item-abc/>
 <item-def/>
 <item-ghj/>
</root>

xml example 2:

<root>
 <item-ghj/>
 <item-ghj/>
 <item-abc/>
</root>

xslt:

<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform">
 <x:template match="/">
  <fo:root text-align="center" font-family="Arial" font-size="9pt">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="m" margin-left="7mm" margin-right="9mm" margin-top="8mm" margin-bottom="1mm" page-width="210mm" page-height="297mm">
            <fo:region-body margin-top="14mm" margin-left="2mm"/>
            <fo:region-before />
            <fo:region-after />
        </fo:simple-page-master>
    </fo:layout-master-set>
   <x:apply-templates select="root/item-abc"/>
   <x:apply-templates select="root/item-def"/>
   <x:apply-templates select="root/item-ghj"/>
  </fo:root>
 </x:template>

 <x:template match="root/item-abc">
  <fo:page-sequence master-reference="m" format="00001">
   ....
  </fo:page-sequence>
 </x:template>

 ...another items' templates
</x:stylesheet>

Solution

  • because dont know which element will be first.

    If it is true, change the follwoing your code

    <x:apply-templates select="root/item-abc"/>
    <x:apply-templates select="root/item-def"/>
    <x:apply-templates select="root/item-ghj"/>
    

    to

    <x:apply-templates select="root/*"/>
    

    And check which is first by position() function:

    <x:template match="root/item-abc">
       <fo:page-sequence master-reference="m" format="00001">
           <x:if test="position() eq 1">
               <x:attribute name="initial-page-number" select="'1'"/>
           </x:if>
       ....
       </fo:page-sequence>
    </x:template>
    

    This will satisfy your requirement.