<?xml version="1.0" ?>
<!DOCTYPE xsl:stylesheet [

]>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
version="1.0">
<!-- Time-stamp: "2000-12-22 11:30:45 dave"  -->
<xsl:template match="/">
<!--  Include the fo:root element, which starts it      -->
<!--  all.                                              -->
<!--                                                    -->
<fo:root>


<!-- This element contains the page geometry and sequencing -->
 <fo:layout-master-set> 

<!--******************************************         -->
<!-- First define the geometry of the pages            -->
<!-- Paper size and layout of the 5 regions            -->
<!-- Page master for the first page, which is special. -->
<!--******************************************         -->

  <xsl:comment>1A</xsl:comment>
<!-- page-master 1. -->
<!-- 'first' is the page-master identifier. 7.24.8 -->
  <fo:simple-page-master              master-name="first"
    page-height="29.7cm" 
    page-width="21cm"
    margin-top="1in"
    margin-bottom="1in"
    margin-left = "0.75in"
    margin-right="0.75in">
<!-- 
body top margin(margin-top) is 'padding size' greater than the extent of the
region before & after. See diagrams in 6.4.12
   -->
 <xsl:comment>1B</xsl:comment>
   <fo:region-body 
     padding-start="1cm"
     padding-end="1cm"
     border-color="black" 
     border-style="solid" 
     border-width="3pt" 
     margin-top="0.6in" 
     margin-bottom="0.6in"/> <!--Region after goes in here.  -->

                        <!-- Header -->
 <xsl:comment>1C</xsl:comment>

   <fo:region-before 
     extent          ="0.5in"/>  <!-- Height of the region -->
 <xsl:comment>1D</xsl:comment>
   <fo:region-after 
     extent          ="0.5in"/>        <!-- Height of region -->
  </fo:simple-page-master>


<!--******************************************-->
<!-- This page-master is used for majority     -->
<!-- of the document                          -->
<!--******************************************-->

 
<!-- page-master 2. -->
  <fo:simple-page-master              master-name="rest"
    page-height="29.7cm" 
    page-width="21cm"
    margin-top="1in"
    margin-bottom="1in"
    margin-left = "0.75in"
    margin-right="0.75in">
<!-- 
body top margin is padding size greater than extent of
region before & after
  -->
 <xsl:comment>1B</xsl:comment>
   <fo:region-body 
   padding="4mm"
   border-color="red" 
   border-style="solid" 
   border-width="1pt" 
   margin-top="0.6in" 
   margin-bottom="0.6in"/> <!--Region after goes in here.  -->

                        <!-- Header -->
 <xsl:comment>1C</xsl:comment>

   <fo:region-before 
     border-color    ="red" 
     border-style    ="solid"
     border-width    ="1pt" 
     extent          ="0.5in"/>  <!-- Height of the region -->

 <xsl:comment>1D</xsl:comment>
   <fo:region-after 
     border-color    ="red" 
     border-style    ="solid"
     border-width    ="1pt" 
     extent          ="0.5in"/>        <!-- Height of region -->
  </fo:simple-page-master> 


<!--******************************************-->
<!--        Two column master - Landscape     -->
<!--******************************************-->
<!-- page-master 3. -->                                       <!--  -->
  <fo:simple-page-master              master-name="twocolsL"
  page-height="29.7cm" 
    page-width="21cm"
    margin-top="1in"
    margin-bottom="1in"
    margin-left = "0.75in"
    margin-right="0.75in">

  <fo:region-body 
    padding="4mm"
    column-count="2"
    column-gap="0.5in"
    reference-orientation="90"
      border="thin silver ridge"
    margin-left="0.6in" 
    margin-right="0.6in"/> <!--Region after goes in here.  -->

 <fo:region-start 
   border-color    ="red" 
   border-style    ="solid"
   border-width    ="1pt" 
   reference-orientation="90"
   extent          ="0.5in"/>  <!-- Height of the region -->

 <fo:region-end 
   border-color    ="red" 
     border-style    ="solid"
     border-width    ="1pt" 
   reference-orientation="90"
   extent          ="0.5in"/>        <!-- Height of region -->
  </fo:simple-page-master>


<!--******************************************-->
<!--        Two column master  -Portrait      -->
<!--******************************************-->
<!-- page-master 4. -->                                       <!--  -->
  <fo:simple-page-master              master-name="twocolsP"
  page-height="29.7cm" 
    page-width="21cm"
    margin-top="1in"
    margin-bottom="1in"
    margin-left = "0.75in"
    margin-right="0.75in">

  <fo:region-body 
    padding-start="1cm"
    padding-end="1cm"
    column-count="2"
    column-gap="0.5in"
    border="thin silver ridge" 
    margin-top="0.6in" 
    margin-bottom="0.6in"/> <!--Region after goes in here.  -->

 <fo:region-before 
   border-color    ="red" 
   border-style    ="solid"
   border-width    ="1pt" 
   extent          ="0.5in"/>  <!-- Height of the region -->

 <fo:region-after 
   border-color    ="red" 
     border-style    ="solid"
     border-width    ="1pt" 
   extent          ="0.5in"/>        <!-- Height of region -->
  </fo:simple-page-master>




<!-- *****************************************************************-->
<!-- Having now specified what the pages look like,                   -->
<!-- We can start to say how they are used,                           -->
<!-- And in which sequence                                            -->
<!-- *****************************************************************-->
<!-- Apologies dear reader. This beast is getting the better of me
     as of today. I want to be able to sequence through various bits
     of the source document, using different masters, but don't know
     how. 

Theory goes that the page-sequences refer to this page-sequence-master
by 'master-name', then the sequencing algorithm selects which page-master
to use by availability and best fit. So says 6.4.7.

With yet more help from Nikolai, I think I now understand it a little better.

Starting from the top. 
I want:
1st page special, a fancy front sheet.
Next few pages normal portrait, headers and footers - The Introduction and section 2
Next section, section 4, is landscape, two columns headers on the left, footers on the right.
Next section, section 5, portrait, two collumns, headers back in their normal place.
Section 6, as per Introduction and section 2.

So, I need a page-master for each different layout.
By layout I mean page dimensions (geometry), header and footer specifications
in terms of borders and sizes (from simple-page-masters) and their content,
from page-sequences. This means that since I don't want headers and footers
on the first page, yet do on the second, I can't put them both in the same
sequence, hence they have to use their own simple-page-master and page-sequence.


Using the page-master master-name attributes I have:
  first - fancy front page
  rest  - Introduction, section 3 and section 6.
  twocolsP - Portrait two columns - section 4
  twocolsL - Landscape two columns - section 5

To achieve this I need to define more than one page-sequence master.

Why?

Listen carefully here, its a long story!
This is the sequence the formatter goes through when you invoke it to lay out
your pages.

In order to determine what content goes into which simple-page-master layout,
The formatter follows the following logic.

(1). Looks for the first page-sequence element.
Get the master-name from the page-sequence.
If it refers to a simple-page-master, layout the content with page geometry
  from the simple-page-master. Keep on doing this until all data exhausted
  from the template-match in the page-sequence.
  When exhausted, try the next page-sequence in document order.
  Repeat from (1), using next page-sequence element

If it refers to a page-sequence-master
(2)  Take the first sub-sequence in the page-sequence master.
  (this could be any one of
(single-page-master-reference|
 repeatable-page-master-reference|
 repeatable-page-master-alternatives)+
  if the 'conditions' expressed in the sub-sequence are met *
     get the simple-page-master referenced from the sub-sequence
     and layout the content with page information
     from the simple-page-master. Keep on doing this until all data exhausted
     from the template-match in the page-sequence or the 'condition' * is 
     no longer valid.
   if the 'conditions' are not met, move on to the next sub-sequence.
   and repeat from (2) using the 'next' sub-sequence.
IMPORTANT:
   Moving on to the next sub-sequence (i.e. not using the same sub-sequence twice)
   only applies to this particular page-sequence. The 'counter' is reset to zero
   once a particular page-sequence is 'used up'.  

* The conditions might be, for instance, odd, even, or first page; Number of pages
  specified in a repeatable-page-master-reference etc.

So having understood how it does it, here goes to determine the content of 
page-sequence-master(s)

For the fancy front page I use 'first', but without any header or footer content.
This matches on section[1]

  

For section 2 I need another page-sequence with match pattern  "/simpdoca/section[2]"
This will reference the second page-sequence-master, which will have content
   repeatable-page-master-reference, which will reference the 'rest' simple-page-master.

For section 3, just a single page, in the page-sequence I will refer directly to
the simple-page-master twocolsL

For section 4, again a single page, in the page-sequence I will refer directly to
the simple-page-master twocolsP.

For section 5, multiple pages, I will refer directly to the simple-page-master 'rest'

Phew. Did you follow the sequence there?
You will need to when you need more than one different page layout!
How about putting that table from 
chapter 5 on a landscape page :-)


  -->

<!--****************************************** -->
<!--       Sequence specification(s)           -->
<!-- This specifies which page-sequence to use -->
<!-- via the master-name.                      -->
<!-- Not used to date!                         -->
<!--****************************************** -->
  <fo:page-sequence-master master-name="sequence-1">
    <fo:repeatable-page-master-reference master-name="first" />
  </fo:page-sequence-master>
 </fo:layout-master-set>




<!-- page-sequence for banner page                             -->
<!-- This selects and causes to be processed,                  -->
<!-- section[1]  of the source document                        -->
<!-- The page-master referenced (via the page-sequence-master  -->
<!-- is the one identified as 'first'                          -->
  <fo:page-sequence  
    master-name="first">
    <xsl:comment>2A</xsl:comment>

        <!-- Header and footer  is empty for first page -->
        <fo:static-content
          flow-name="xsl-region-before"><fo:block></fo:block></fo:static-content>
        <fo:static-content
          flow-name="xsl-region-after"><fo:block></fo:block></fo:static-content>
        <fo:flow flow-name="xsl-region-body"> 
        <xsl:comment>2D</xsl:comment>
      <!-- Process section contents -->
      <xsl:apply-templates select="/simpdoca/section[1]" />
      </fo:flow>
    </fo:page-sequence>

<!-- page-sequence for portrait pages                          -->
<!-- This selects and causes to be processed,                  -->
<!-- section[3]  of the source document          -->
<!-- The page-master referenced (via the page-sequence-master  -->
<!-- is the one identified as 'first'                           -->
  <fo:page-sequence  
    master-name="rest">
    <xsl:comment>2A</xsl:comment>
   <xsl:call-template name="page-sequence">
     <xsl:with-param name="region-head">before</xsl:with-param>
     <xsl:with-param name="region-foot">after</xsl:with-param>
     <xsl:with-param name="head-L">An XSL Primer</xsl:with-param>
     <xsl:with-param name="head-R">Page <fo:page-number/> </xsl:with-param>
     <xsl:with-param name="foot-L">XSL Primer</xsl:with-param>
     <xsl:with-param name="foot-R">Page <fo:page-number/></xsl:with-param>
   </xsl:call-template>
        <fo:flow flow-name="xsl-region-body"> 
        <xsl:comment>2D</xsl:comment>
      <!-- Process section contents -->
      <xsl:apply-templates select="/simpdoca/section[2]|/simpdoca/section[3]" />
      </fo:flow>
    </fo:page-sequence>






<!-- page-sequence for rotated pages. Landscape                -->
<!-- This selects and causes to be processed,                  -->
<!-- section[3]  of the source document                        -->
<!-- The page-master referenced  directly                      -->
<!-- is the one identified as 'twocolsL                        -->
 <fo:page-sequence  
    master-name="twocolsL">
   <xsl:call-template name="page-sequence">
     <xsl:with-param name="region-head">start</xsl:with-param>
     <xsl:with-param name="region-foot">end</xsl:with-param>
     <xsl:with-param name="head-L">An XSL Primer</xsl:with-param>
     <xsl:with-param name="head-R">Page <fo:page-number/> </xsl:with-param>
     <xsl:with-param name="foot-L">XSL Primer</xsl:with-param>
     <xsl:with-param name="foot-R">Page <fo:page-number/></xsl:with-param>
   </xsl:call-template>



 <fo:flow flow-name="xsl-region-body"> 
    <xsl:apply-templates select="/simpdoca/section[4]"/>
   </fo:flow>
 </fo:page-sequence>


<!-- page-sequence for two-cols, portrait pages                -->
<!-- page-sequence for Portait pages with two columns          -->
<!-- This selects and causes to be processed,                  -->
<!-- section[4]  of the source document                        -->
<!-- The page-master referenced  directly                      -->
<!-- is the one identified as 'twocolsP                        -->







  <fo:page-sequence  
    master-name="twocolsP">
    <xsl:comment>??</xsl:comment>

<xsl:call-template name="page-sequence">
  <xsl:with-param name="head-L">An XSL Primer</xsl:with-param>
  <xsl:with-param name="head-R">Page <fo:page-number/></xsl:with-param>
  <xsl:with-param name="foot-L">XSL Primer</xsl:with-param>
  <xsl:with-param name="foot-R">Page <fo:page-number/></xsl:with-param>
</xsl:call-template>


  <fo:flow flow-name="xsl-region-body"> 
     <xsl:comment>2D</xsl:comment>
    <!-- Process section contents -->
    <xsl:apply-templates select="/simpdoca/section[5]" />
   </fo:flow>
</fo:page-sequence>


<!-- This page-sequence is called up by all sections after 4 -->
<!-- Note it must be last page-sequence, since it picks up all sections after 5 -->

  <fo:page-sequence  
    master-name="rest">
    <xsl:comment>2A</xsl:comment>
<!-- Header and footer stuff is constant for any particular orientation. -->

<xsl:call-template name="page-sequence">
  <xsl:with-param name="head-L">An XSL Primer</xsl:with-param>
  <xsl:with-param name="head-R">Page <fo:page-number/> </xsl:with-param>
  <xsl:with-param name="foot-L">XSL Primer</xsl:with-param>
  <xsl:with-param name="foot-R">Page <fo:page-number/></xsl:with-param>
</xsl:call-template>
  <fo:flow flow-name="xsl-region-body"> 
     <xsl:comment>2D</xsl:comment>
    <!-- Process section contents -->
    <xsl:apply-templates select="simpdoca/section[position() &gt;5]" />
   </fo:flow>
</fo:page-sequence>
 </fo:root>
</xsl:template>


  <!-- This named template can be used to generate headers and
     footers, for any page-sequence. -->
  <xsl:template name="page-sequence">
    <xsl:param name="region-head">before</xsl:param><!-- region name for header -->
    <xsl:param name="region-foot">after</xsl:param><!-- region name for footer -->
    <xsl:param name="head-L"> </xsl:param>
    <xsl:param name="head-R">Page <fo:page-number/></xsl:param>
    <xsl:param name="foot-L"> </xsl:param>
    <xsl:param name="foot-R"><fo:page-number/></xsl:param>

<!-- header section  -->
   <fo:static-content 
	 flow-name="{concat('xsl-region-',$region-head)}">          
       <xsl:comment>2B</xsl:comment>
	 <fo:list-block  
	   font-family="Helvetica" 
	   font-size="10pt" 
	   provisional-distance-between-starts="5in" 
	   provisional-label-separation="0in">
	   <fo:list-item>
	     <fo:list-item-label>
               <!--	       <fo:block 
		 text-align="start"><xsl:value-of select="$head-L"/>--><!-- Header Left value -->
               <!--    </fo:block> retrieve-boundary="page-sequence" retrieve-position="first-starting-within-page " -->
               <fo:retrieve-marker
                 retrieve-class-name="sect-head"
                 >
               </fo:retrieve-marker>

	     </fo:list-item-label>
	     <fo:list-item-body>
	       <fo:block
                 text-align="end">
                 <xsl:copy-of select="$head-R"/> </fo:block><!-- Header Right -->
	       </fo:list-item-body>
	     </fo:list-item>    
	   </fo:list-block>
	 </fo:static-content>

<!-- Footer -->

  <fo:static-content  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		     xmlns:fo="http://www.w3.org/1999/XSL/Format"
    flow-name="{concat('xsl-region-',$region-foot)}">
  <fo:list-block  
		 font-family="Helvetica" 
		 font-size="10pt" 
		 provisional-distance-between-starts="5in" 
		 provisional-label-separation="0in">
    <fo:list-item>
     <fo:list-item-label>
       <fo:block text-align="start" ><xsl:copy-of select="$foot-L"/>
      </fo:block>
     </fo:list-item-label>
     <fo:list-item-body>
      <fo:block
              text-align="end"><xsl:copy-of select="$foot-R"/></fo:block>
     </fo:list-item-body>
    </fo:list-item>    
   </fo:list-block>
  </fo:static-content>
  </xsl:template>



</xsl:stylesheet> 