<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <!-- *** main template*** -->
  <xsl:template match="book">
    <fo:root>
      <fo:layout-master-set>
         <fo:simple-page-master master-name="normal"
                                page-height="841.9pt"
                                page-width="595.3pt"
                                margin-top="112.42pt"
                                margin-bottom="69.48pt"
                                margin-left="99.88pt"
                                margin-right="99.88pt">
          <fo:region-body margin-bottom="20pt"
                          background-color="lightgrey"
                          display-align="before"/>
          <fo:region-after extent="20pt"/>
        </fo:simple-page-master>

         <fo:simple-page-master master-name="titlepage"
                                page-height="841.9pt"
                                page-width="595.3pt"
                                margin-top="112.42pt"
                                margin-bottom="69.48pt"
                                margin-left="99.88pt"
                                margin-right="99.88pt">
          <fo:region-body margin-bottom="20pt"
                          display-align="center"/>
          <fo:region-after extent="20pt"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="titlepage">
        <fo:flow flow-name="xsl-region-body"
                 language="it"
                 hyphenate="true"
                 font-family="Times Roman">
          <fo:block text-align="center"
                    font-weight="bold"
                    start-indent="2cm"
                    end-indent="2cm"><xsl:value-of select="@title"/></fo:block>
          <fo:block text-align="center"
                    font-style="italic"
                    start-indent="2cm"
                    end-indent="2cm">by <xsl:value-of select="@author"/></fo:block>
        </fo:flow>
      </fo:page-sequence>

      <fo:page-sequence master-reference="normal">
        <fo:flow flow-name="xsl-region-body"
                 language="it"
                 hyphenate="true"
                 font-family="Times Roman">
          <xsl:apply-templates select="chapter"/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <!-- *** other templates*** -->

  <xsl:template match="chapter">
    <xsl:apply-templates select="chapter-number"/>
    <xsl:apply-templates select="chapter-title"/>
    <xsl:apply-templates select="par[1]" mode="primo"/>
  </xsl:template>

  <xsl:template match="chapter-number">
    <fo:block break-before="odd-page"
              space-before="60pt"
              space-before.conditionality="retain"
              keep-with-next.within-page="always"
              text-align="center"
              hyphenate="false"
              font-weight="bold"
              line-height="20pt"
              line-stacking-strategy="font-height"
              background-color="white"><xsl:apply-templates/></fo:block>
  </xsl:template>

  <xsl:template match="chapter-title">
    <fo:block space-before="20pt"
              space-after="60pt"
              space-before.conditionality="retain"
              space-after.conditionality="retain"
              keep-with-next.within-page="always"
              text-align="center"
              hyphenate="false"
              line-height="20pt"
              line-stacking-strategy="font-height"
              background-color="white"><xsl:apply-templates/></fo:block>
  </xsl:template>

  <!--   first paragraph after a title   -->
  <xsl:template match="par" mode="primo">
    <fo:block text-align="justify"
              line-height="20pt"
              line-stacking-strategy="font-height"
              background-color="white">
      <xsl:apply-templates/>
    </fo:block>
    <xsl:apply-templates select="following-sibling::*[self::par][1]"/>
  </xsl:template>

  <!-- paragraph after another paragraph -->
  <xsl:template match="par">
    <fo:block text-align="justify"
              text-indent="0.5cm"
              line-height="20pt"
              line-stacking-strategy="font-height"
              background-color="white">
      <xsl:apply-templates/>
    </fo:block>
    <xsl:apply-templates select="following-sibling::*[self::par][1]"/>
  </xsl:template>

</xsl:stylesheet> 
