Tuesday, December 30, 2014

Apache FOP Tutorial Part 2

<<< Part 1 <<<                                                                                                                >>> Part 3 >>>

Let's now write a XSL stylesheet which will convert the data XML into a XSL FO tree. The XSL stylesheet looks as follows. This basically tells about how the data layout will happen.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"       
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="fo">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes" />

<xsl:param name="versionParam" select="'1.0'" />
<xsl:template match="Students">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4"
page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="1cm">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="14pt" font-weight="bold" space-after="5mm">
List of Students:
<xsl:value-of select="Name" />
</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed" width="100%"
border-collapse="separate">
<fo:table-column column-width="6cm" />
<fo:table-column column-width="3cm" />
<fo:table-body>
<xsl:apply-templates select="Student" />
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="Student">
  <fo:table-row>
  <fo:table-cell>
<fo:block>
<xsl:value-of select="Name" />
</fo:block>
  </fo:table-cell>
  <fo:table-cell>
  <fo:block>
<xsl:value-of select="Age" />
  </fo:block>
  </fo:table-cell>
  </fo:table-row>
</xsl:template>
</xsl:stylesheet>

<<< Part 1 <<<                                                                                                                >>> Part 3 >>>

No comments:

Post a Comment