<?xml version="1.0"	encoding="UTF-8"?>

<!--
 This is a more involved style-sheet. It demonstrates
 more of the capabilities of XSLT/XSLFO.
-->


<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
				xmlns:fo="http://www.w3.org/1999/XSL/Format"
				version="1.0">

 <xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/>

<!--********************************************************-->
<!-- A callable template which generates the page masters.  -->
<!--********************************************************-->
	<xsl:template name="doPageMasters">
		<fo:layout-master-set>

			<fo:simple-page-master master-name="main"  margin-left="72pt" margin-right="72pt" margin="18pt">
				<fo:region-before region-name="before" extent="36pt" />
				<fo:region-after region-name="after" extent="36pt" />
				<fo:region-body	margin-top="36pt" margin-bottom="36pt"/>
		 </fo:simple-page-master>

		</fo:layout-master-set>
	</xsl:template>


<!--********************************************************-->
<!-- Some simple, html type styling tags.                   -->
<!--********************************************************-->
	<xsl:template match="b">
		<fo:inline font-weight="bold">
			<xsl:apply-templates />
		</fo:inline>
	</xsl:template>

	<xsl:template match="big">
		<fo:inline font-size="24pt" space-end="-3pt">
			<xsl:apply-templates />
		</fo:inline>
	</xsl:template>

	<xsl:template match="i">
		<fo:inline font-style="italic">
			<xsl:apply-templates />
		</fo:inline>
	</xsl:template>


	<xsl:template match="p">
		<fo:block space-before="6pt">
			<xsl:apply-templates />
		</fo:block>
	</xsl:template>


<!--********************************************************-->
<!-- Process a Chapter.                                     -->
<!--********************************************************-->
	<xsl:template match="chapter">
		<fo:block break-before="page" space-before="6pt" space-after="4pt">
			<fo:marker marker-class-name="ChapTitle"><xsl:value-of select="@title" /></fo:marker>
			<xsl:apply-templates />
		</fo:block>
	</xsl:template>


<!--********************************************************-->
<!-- Process all of the chapters in the document.           -->
<!--********************************************************-->
	<xsl:template name="doChapters">
		<fo:page-sequence master-reference="main">

			<!-- Page Heading -->
			<fo:static-content flow-name="before">
				<fo:block text-align-last="justify" space-after="6pt">
					<fo:wrapper font-size="14pt">
						<fo:retrieve-marker retrieve-class-name="ChapTitle"/>
					</fo:wrapper>
					<fo:leader />
					Page <fo:page-number/> of <fo:page-number-citation ref-id="lastPageId" />
				</fo:block>

				<fo:block line-height="2pt" text-align-last="justify">
					<fo:leader leader-pattern="rule" />
				</fo:block>

			</fo:static-content>


			<!-- Page Footer -->
			<fo:static-content flow-name="after">
				<fo:block text-align-last="justify" >
					<fo:leader leader-pattern="rule" alignment-adjust="central" />
					<fo:page-number space-start="8pt" space-end="8pt"/>
					<fo:leader leader-pattern="rule" alignment-adjust="central" />
				</fo:block>

			</fo:static-content>



			<!-- The main flow... -->
			<fo:flow flow-name="xsl-region-body">

				<!-- Process the Chapters... -->
				<xsl:apply-templates />

				<!-- identify the 'last' page. -->
				<fo:block id="lastPageId" line-height="0" />

			</fo:flow>
		</fo:page-sequence>

	</xsl:template>


<!--********************************************************-->
<!-- Start at the root of the Document.                     -->
<!--********************************************************-->
	<xsl:template match = "document">
		<fo:root	xmlns:fo="http://www.w3.org/1999/XSL/Format">
			<xsl:call-template name="doPageMasters" />
			<xsl:call-template name="doChapters" />
		</fo:root>
	</xsl:template>



</xsl:stylesheet>
