Tutorials : Using FOP with Java - Part 2 :

Next we will present the sample XML andXSL files in order to the FOP processing. Listing 2 presents the sample.xml file and Listing 3 presents the sample XSL FO file for the processing.


<?xml version="1.0" encoding="UTF-8"?>
<Catalogue>
<Book>
  <Title>Mastering EJB</Title>
  <Author>Ed Roman</Author>
  <Price>$45.00</Price>
</Book>
<Book>
  <Title>Design Patterns</Title>
  <Author>Erich Gamma</Author>
  <Price>$50.00</Price>
</Book>
<Book>
  <Title>Effective Java</Title>
  <Author>Josch Bloch</Author>
  <Price>$30.00</Price>
</Book>
 
</Catalogue>

Listing 2 , a sample XML file


Below is the sample XSL file to use for the XSL -FO formatting.


<?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" xmlns:java="java" exclude-result-prefixes="fo">    
        <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
        <!-- ========================= -->
        <!-- root element: Catalogue-->
        <!-- ========================= -->
        <xsl:template match="Catalog">
                <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                        <fo:layout-master-set>
<!-- (1. Define the page margins) -->
                                <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="2cm">
                                        <fo:region-body/>
                                </fo:simple-page-master>
                        </fo:layout-master-set>
<!-- (2. For the page layout refer to the master layout)-->
                        <fo:page-sequence master-reference="simpleA4">
                                <fo:flow flow-name="xsl-region-body">
                                        <fo:block font-size="16pt" font-weight="bold" space-after="5mm">Catalog Information
                                        </fo:block>
 
<!-(3. Defining the block with table definition to display data-->

                                        <fo:block font-size="10pt">
                                                <fo:table table-layout="fixed">
                                                        <fo:table-column column-width="8cm"/>
                                                        <fo:table-column column-width="8cm"/>
                                                        <fo:table-column column-width="8cm"/>
                                                        <fo:table-body>
                                                                <xsl:apply-templates/>
                                                        </fo:table-body>
                                                </fo:table>
                                        </fo:block>
                                </fo:flow>
                        </fo:page-sequence>
                </fo:root>
        </xsl:template>
 
        <!-- ========================= -->
        <!-Book Information    -->
        <!-- ========================= -->
        
        <xsl:template match="Book">
         <fo:table-row>
                <fo:table-cell>
                        <fo:block>
                          <xsl:value-of select="Title"/>
                        </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                        <fo:block>
                           <xsl:value-of select="Author"/>
                        </fo:block>
                </fo:table-cell>
            <fo:table-cell>
                        <fo:block>
                           <xsl:value-of select="Price"/>
                        </fo:block>
                </fo:table-cell>
 
 
        </fo:table-row>
        </xsl:template> 
        
</xsl:stylesheet>
Listing 3: sample XSL file to format the XML data

Executing this program will print the document to the default printer installed in your computer.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.