Home arrow XML arrow Page 9 - Introduction to Cocoon, XML XSL

Viewing your file as a pdf - XML

This tutorial will guide you through setting up Tomcat and Cocoon to serve XML pages, then you will create a DTD, XML file and three XSL stylesheets so that you can view your data in your desktop browser, a cell phone browser and a pdf file.

TABLE OF CONTENTS:
  1. Introduction to Cocoon, XML XSL
  2. Getting the tools
  3. Installing Tomcat
  4. Installing Cocoon
  5. Defining your document
  6. Creating your xml file
  7. Viewing your document in an HTML browser
  8. Viewing your document in a WAP browser
  9. Viewing your file as a pdf
By: Olivier Eymere
Rating: starstarstarstarstar / 9
March 22, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The last thing we do in this tutorial is convert the xml to a pdf file. Creating a pdf file is great when your users might want to keep or print a copy of the data. Since pdfs are not editable they are a great way to provide users with a document that they will not be able to accidentally (or purposely) modify later.

For pdf generation we will use FOP from Apache. FOP takes in an xml document, an FO stylesheet and outputs a pdf. FOP is actually a separate project from cocoon but the FOP jar is included in the cocoon distribution so you do not need to add anything new to the project. There are a lot of similarities with html or wml stylesheets but you will notice that the formatting is much more specific. You need to create a file called address-pdf.xsl and open it in a text browser. Begin the file with the usual xsl:stylesheet PI but you also need to say that you will be using the xml namespaces for xml and fo:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

Use an xsl template to match the contact item and add a processing-instruction for the type text/xslfo.

<xsl:template match="contact"> <xsl:processing-instruction name="cocoon-format"> type="text/xslfo"</xsl:processing-instruction>

For any fo file you first need an fo:root element. The fo:root element is essentially the same as the root xsl:template. It defines page layouts, page sequences and host of optional text formatting instructions.


<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

Every fo:root must have one fo:layout-master-set. The fo:layout-master-set defines the layout specification including page margins.


<fo:layout-master-set> <fo:simple-page-master page-master-name="single" margin-top="2cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-bottom="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set>

Next add an fo:page-sequence to define how to create the pages within the document. Your entire document can be within one fo:page-sequence or you can use different page-sequence for sections.


<fo:page-sequence> <fo:sequence-specification> <fo:sequence-specifier-alternating page-master-first="single" page-master-odd="single" page-master-even="single"/> </fo:sequence-specification>

Next you need to add fo:flow to output the text. In our file we are applying the templates from the xml result tree.


<fo:flow> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> Close the fo:root and xsl:template </fo:root> </xsl:template>

Finally we create xsl:templates for the elements of our contact item. In each template we have an fo:block which is used to define font, font size, alignment, etc.


<xsl:template match="name"> <fo:block font-size="12pt" text-align="justified"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="address"> <fo:block font-size="12pt" text-align="justified"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="phone"> <fo:block font-size="12pt" text-align-last="justified"> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="e-mail"> <fo:block font-size="12pt" space-before.optimum="12pt" text-align="justified"> <xsl:apply-templates/> </fo:block> </xsl:template> </xsl:stylesheet>

You will notice that the xsl syntax is similar to previous xml files but with fo formatting instruction. Now all that needs to be done is to tell your xml file to use address-pdf.xsl. For simplicity open homer.xml and replace:


<?xml-stylesheet href="address-html.xsl" type="text/xsl"?>

with:

<?xml-stylesheet href="address-pdf.xsl" type="text/xsl"?>

It is possible to have both the pdf and html file generated at the same time and have the pdf downloaded from a link in the html page but it is not a straight forward as you might think. I will save that for another day. All that is left for you now is to point your browser to http://localhost:8080/address/homer.xml. If you have an Acrobat plugin the pdf should load automatically. If you do not have a plugin you should be prompted to download the file. Once downloaded open Acrobat to view the file.

That covers this tutorial. If everything has gone well you have a good idea of how powerful xml can be. Hopefully it can help you manage your data and make your life a little easier



 
 
>>> More XML Articles          >>> More By Olivier Eymere
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: