<?xml version="1.0" encoding="ISO-8859-1" ?>

<!--
 ####################################################################
 #
 # mrml2xhtml.xsl
 #
 # Author:		Daniel Easton
 # Created:		20 October 2003
 # Comments:
 #
 # Converts MRML into XMLFO for FOP formatter
 #
 # History:
 #
 # Version 0.1 (20/10/03) - Creation
 #
 ####################################################################
-->

<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 indent="yes" method="xml" encoding="UTF-8" />

<!-- page length units -->
<xsl:param name="units">mm</xsl:param>

<!-- page size -->
<xsl:param name="pagewidth">210</xsl:param>
<xsl:param name="pageheight">297</xsl:param>

<!-- page margins -->
<xsl:param name="pagemargintop">20</xsl:param>
<xsl:param name="pagemarginright">20</xsl:param>
<xsl:param name="pagemarginbottom">20</xsl:param>
<xsl:param name="pagemarginleft">20</xsl:param>

<!-- header size -->
<xsl:param name="headerheight">20</xsl:param>

<!-- header margins -->
<xsl:param name="headermargintop">20</xsl:param>
<xsl:param name="headermarginright">0</xsl:param>
<xsl:param name="headermarginbottom">0</xsl:param>
<xsl:param name="headermarginleft">0</xsl:param>

<!-- footer size -->
<xsl:param name="footerheight">10</xsl:param>

<!-- footer margins -->
<xsl:param name="footermargintop">0</xsl:param>
<xsl:param name="footermarginright">0</xsl:param>
<xsl:param name="footermarginbottom">0</xsl:param>
<xsl:param name="footermarginleft">0</xsl:param>


<!-- body margins -->
<xsl:param name="bodymargintop">0</xsl:param>
<xsl:param name="bodymarginright">1</xsl:param>
<xsl:param name="bodymarginbottom">1</xsl:param>
<xsl:param name="bodymarginleft">1</xsl:param>

<!-- body area calculations -->
<xsl:variable name="bodywidth">
	<xsl:value-of select="$pagewidth - ($pagemarginright + $pagemarginleft)" />
</xsl:variable>
<xsl:variable name="bodyavailablewidth">
	<xsl:value-of select="$pagewidth - ($pagemarginright + $pagemarginleft + $bodymarginright + $bodymarginleft)" />
</xsl:variable>

<xsl:template match="/">

	<fo:root>

		<fo:layout-master-set>

			<fo:simple-page-master master-name="PageMaster">
				<xsl:attribute name="page-height"><xsl:value-of select="concat($pageheight, $units)" /></xsl:attribute>
				<xsl:attribute name="page-width"><xsl:value-of select="concat($pagewidth, $units)" /></xsl:attribute>
				<xsl:attribute name="margin-top"><xsl:value-of select="concat($pagemargintop, $units)" /></xsl:attribute>
				<xsl:attribute name="margin-right"><xsl:value-of select="concat($pagemarginright, $units)" /></xsl:attribute>
				<xsl:attribute name="margin-bottom"><xsl:value-of select="concat($pagemarginbottom, $units)" /></xsl:attribute>
				<xsl:attribute name="margin-left"><xsl:value-of select="concat($pagemarginleft, $units)" /></xsl:attribute>

				<fo:region-body>
					<xsl:attribute name="margin-top"><xsl:value-of select="concat(($bodymargintop+$headerheight), $units)" /></xsl:attribute>
					<xsl:attribute name="margin-right"><xsl:value-of select="concat($bodymarginright, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-bottom"><xsl:value-of select="concat($bodymarginbottom, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-left"><xsl:value-of select="concat($bodymarginleft, $units)" /></xsl:attribute>
				</fo:region-body>

				<fo:region-before display-align="after">
					<xsl:attribute name="extent"><xsl:value-of select="concat($headerheight, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-top"><xsl:value-of select="concat($headermargintop, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-right"><xsl:value-of select="concat($headermarginright, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-bottom"><xsl:value-of select="concat($headermarginbottom, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-left"><xsl:value-of select="concat($headermarginleft, $units)" /></xsl:attribute>
				</fo:region-before>

				<fo:region-after display-align="before">
					<xsl:attribute name="extent"><xsl:value-of select="concat($footerheight, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-top"><xsl:value-of select="concat($footermargintop, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-right"><xsl:value-of select="concat($footermarginright, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-bottom"><xsl:value-of select="concat($footermarginbottom, $units)" /></xsl:attribute>
					<xsl:attribute name="margin-left"><xsl:value-of select="concat($footermarginleft, $units)" /></xsl:attribute>
				</fo:region-after>

			</fo:simple-page-master>

			<fo:page-sequence-master master-name="ReportMaster">
				<fo:repeatable-page-master-reference master-reference="PageMaster" />
			</fo:page-sequence-master>

		</fo:layout-master-set>

		<xsl:apply-templates select="report" />

	</fo:root>

</xsl:template>

<xsl:template match="report">
	<fo:page-sequence master-reference="ReportMaster" initial-page-number="1">

		<fo:static-content flow-name="xsl-region-before">

			<fo:block>
				<fo:table table-layout="fixed">
					<xsl:attribute name="width"><xsl:value-of select="concat($bodywidth, $units)" /></xsl:attribute>
					<xsl:attribute name="height"><xsl:value-of select="concat($headerheight, $units)" /></xsl:attribute>

					<xsl:for-each select="sections/header-section/header-layout/column">
						<fo:table-column>
							<xsl:attribute name="column-width">
								<xsl:value-of select="concat($bodywidth*@width, $units)" />
							</xsl:attribute>
						</fo:table-column>
					</xsl:for-each>

					<fo:table-body>

						<xsl:for-each select="sections/header-section/header/hr">
							<fo:table-row>
								<xsl:for-each select="hd">
									<fo:table-cell>
										<xsl:if test="@rowspan &gt; 0">
											<xsl:attribute name="number-rows-spanned">
												<xsl:value-of select="@rowspan" />
											</xsl:attribute>
										</xsl:if>

										<xsl:if test="@colspan &gt; 0">
											<xsl:attribute name="number-columns-spanned">
												<xsl:value-of select="@colspan" />
											</xsl:attribute>
										</xsl:if>

										<fo:block>
											<xsl:if test="@class">
												<xsl:variable name="classname" select="@class" />

												<xsl:if test="/report/styles/class[@name=$classname]">
													<xsl:for-each select="/report/styles/class[@name=$classname]">
														<xsl:for-each select="style">

															<xsl:attribute name="{@name}">
																<xsl:value-of select="." />
															</xsl:attribute>

														</xsl:for-each>
													</xsl:for-each>

												</xsl:if>
											</xsl:if>

											<xsl:if test="@type='image'">
												<fo:external-graphic>
													<xsl:attribute name="src">
														<xsl:value-of select="concat(.,'.tif')" />
													</xsl:attribute>
													<xsl:attribute name="height">10mm</xsl:attribute>
												</fo:external-graphic>
											</xsl:if>

											<xsl:if test="not(@type='image')">
												<xsl:value-of select="." />
											</xsl:if>

										</fo:block>

									</fo:table-cell>
								</xsl:for-each>
							</fo:table-row>
						</xsl:for-each>

					</fo:table-body>
				</fo:table>
			</fo:block>
		</fo:static-content>

		<fo:static-content flow-name="xsl-region-after">
			<fo:block>
				<fo:table table-layout="fixed">
					<xsl:attribute name="width"><xsl:value-of select="concat($bodywidth, $units)" /></xsl:attribute>
					<xsl:attribute name="height"><xsl:value-of select="concat($footerheight, $units)" /></xsl:attribute>

					<xsl:for-each select="sections/footer-section/footer-layout/column">
						<fo:table-column>
							<xsl:attribute name="column-width">
								<xsl:value-of select="concat($bodywidth*@width, $units)" />
							</xsl:attribute>
						</fo:table-column>
					</xsl:for-each>

					<fo:table-body>

						<xsl:for-each select="sections/footer-section/footer/fr">
							<fo:table-row>
								<xsl:for-each select="fd">
									<fo:table-cell>
										<xsl:if test="@rowspan &gt; 0">
											<xsl:attribute name="number-rows-spanned">
												<xsl:value-of select="@rowspan" />
											</xsl:attribute>
										</xsl:if>

										<xsl:if test="@colspan &gt; 0">
											<xsl:attribute name="number-columns-spanned">
												<xsl:value-of select="@colspan" />
											</xsl:attribute>
										</xsl:if>

										<fo:block>
											<xsl:if test="@class">
												<xsl:variable name="classname2" select="@class" />

												<xsl:if test="/report/styles/class[@name=$classname2]">
													<xsl:for-each select="/report/styles/class[@name=$classname2]">
														<xsl:for-each select="style">

															<xsl:attribute name="{@name}">
																<xsl:value-of select="." />
															</xsl:attribute>

														</xsl:for-each>
													</xsl:for-each>

												</xsl:if>
											</xsl:if>

											<xsl:if test="@type='image'">
												<fo:external-graphic>
													<xsl:attribute name="src">
														<xsl:value-of select="concat(.,'.tif')" />
													</xsl:attribute>
													<xsl:attribute name="height">10mm</xsl:attribute>
												</fo:external-graphic>
											</xsl:if>

											<xsl:if test="@type='date'">
												<xsl:value-of select="." />
											</xsl:if>

											<xsl:if test="@type='pagenumber'">
												<xsl:if test="styles">
													<xsl:apply-templates select="styles" />
												</xsl:if>

												Page <fo:page-number />

											</xsl:if>

										</fo:block>

									</fo:table-cell>
								</xsl:for-each>
							</fo:table-row>
						</xsl:for-each>

					</fo:table-body>
				</fo:table>

			</fo:block>

		</fo:static-content>

		<fo:flow flow-name="xsl-region-body">

			<xsl:for-each select="sections/section">
				<fo:block>
					<xsl:attribute name="padding-bottom">6px</xsl:attribute>
					<xsl:attribute name="padding-top">6px</xsl:attribute>
					<xsl:attribute name="border-bottom-style">solid</xsl:attribute>
					<xsl:attribute name="border-bottom-color">#c0c0c0</xsl:attribute>
					<xsl:attribute name="border-bottom-width">thin</xsl:attribute>

					<fo:table table-layout="fixed">
						<xsl:attribute name="table-width">
							<xsl:value-of select="concat($bodyavailablewidth, $units)" />
						</xsl:attribute>

						<xsl:for-each select="/report/sections/section-layout/column">
							<fo:table-column>
								<xsl:attribute name="column-width">
									<xsl:value-of select="concat($bodyavailablewidth*@width, $units)" />
								</xsl:attribute>
							</fo:table-column>
						</xsl:for-each>

						<fo:table-body>
							<xsl:call-template name="datasection" />
						</fo:table-body>

					</fo:table>

				</fo:block>
			</xsl:for-each>

		</fo:flow>
	</fo:page-sequence>
</xsl:template>

<xsl:template name="datasection">
	<xsl:for-each select="sr">
		<fo:table-row>
			<xsl:apply-templates select="sd" />
		</fo:table-row>
	</xsl:for-each>
</xsl:template>

<xsl:template match="styles">
	<xsl:for-each select="style">
		<xsl:attribute name="{@name}">
			<xsl:value-of select="." />
		</xsl:attribute>
	</xsl:for-each>
</xsl:template>

<xsl:template match="sd">
	<fo:table-cell>
		<xsl:if test="@rowspan &gt; 0">
			<xsl:attribute name="number-rows-spanned">
				<xsl:value-of select="@rowspan" />
			</xsl:attribute>
		</xsl:if>

		<xsl:if test="@colspan &gt; 0">
			<xsl:attribute name="number-columns-spanned">
				<xsl:value-of select="@colspan" />
			</xsl:attribute>
		</xsl:if>

		<xsl:if test="@class='sectiondatatitle'">
			<xsl:attribute name="font-weight">bold</xsl:attribute>
		</xsl:if>

		<fo:block font-size="9px">

			<xsl:if test="not(table)">
				<xsl:value-of select="." />
			</xsl:if>

			<xsl:for-each select="table">
				<fo:block>
					<fo:table table-layout="fixed">
						<xsl:attribute name="table-width">
							<xsl:value-of select="concat(95, 'mm')" />
						</xsl:attribute>

						<xsl:for-each select="tr/th">
							<fo:table-column>
								<xsl:attribute name="column-width">
									<xsl:value-of select="concat(95*0.2, 'mm')" />
								</xsl:attribute>
							</fo:table-column>
						</xsl:for-each>

						<fo:table-body>
							<xsl:call-template name="subquerytable" />
						</fo:table-body>

					</fo:table>

				</fo:block>
			</xsl:for-each>

		</fo:block>

	</fo:table-cell>
</xsl:template>

<xsl:template name="subquerytable">
	<xsl:for-each select="tr">
		<fo:table-row>
			<xsl:call-template name="headercolumns" />
			<xsl:call-template name="subquerytabledata" />
			<xsl:apply-templates select="td" />
		</fo:table-row>
	</xsl:for-each>
</xsl:template>

<xsl:template name="headercolumns">
	<xsl:for-each select="th">
		<fo:table-cell>
			<xsl:if test="@rowspan &gt; 0">
				<xsl:attribute name="number-rows-spanned">
					<xsl:value-of select="@rowspan" />
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="@class">
				<xsl:attribute name="class">
					<xsl:value-of select="@class" />
				</xsl:attribute>
			</xsl:if>

			<fo:block font-size="9px" text-align="end" wrap-option="no-wrap" font-weight="bold">
				<xsl:value-of select="." />
			</fo:block>

		</fo:table-cell>
	</xsl:for-each>
</xsl:template>


<xsl:template name="subquerytabledata">
	<xsl:for-each select="td">
		<fo:table-cell>
			<xsl:if test="@rowspan &gt; 0">
				<xsl:attribute name="number-rows-spanned">
					<xsl:value-of select="@rowspan" />
				</xsl:attribute>
			</xsl:if>

			<xsl:if test="@class">
				<xsl:attribute name="class">
					<xsl:value-of select="@class" />
				</xsl:attribute>
			</xsl:if>



			<fo:block font-size="9px" text-align="end">
				<xsl:value-of select="." />
			</fo:block>

		</fo:table-cell>
	</xsl:for-each>

</xsl:template>

</xsl:stylesheet>
