XML
  Home arrow XML arrow Page 9 - Introduction to Cocoon, XML XSL
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Introduction to Cocoon, XML XSL
By: Olivier Eymere
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2001-03-22

    Table of Contents:
  • Introduction to Cocoon, XML XSL
  • Getting the tools
  • Installing Tomcat
  • Installing Cocoon
  • Defining your document
  • Creating your xml file
  • Viewing your document in an HTML browser
  • Viewing your document in a WAP browser
  • Viewing your file as a pdf

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Introduction to Cocoon, XML XSL - Viewing your file as a pdf
    (Page 9 of 9 )

    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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway