Exercise 5: Creating PDF output for the résumé using XSL-FO

In this exercise, you’ll create a transformation that will transform your XML résumé into PDF output.

Mapping the XML elements to formatting seems more complicated, because FO tends to be more verbose than simple HTML. The principle is the same: mapping a set of XML tags (in the résumé) to the output tags we want.

As an example, here again is the <school> element from the XML résumé example.

<resume>
   <!-- complete content goes here -->
   <school type=”college”>
       <name>Austin Community College</name>
       <degree>Associates of Applied Science</degree>
       <year>2013</year>
       <location>
           <city>Austin</city>
           <state>Texas</state>
      </location>
   </school>
</resume>

Step 1: Create the FO framework

In Oxygen, select New... > Framework templates > FO > Formatting Objects to create a new file. oXygen creates a file that contains the following code:
<?xml version="1.0" encoding="UTF-8"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set> 
       <fo:simple-page-master master-name=""> 
         <fo:region-body>
         </fo:region-body> 
       </fo:simple-page-master> 
      </fo:layout-master-set> 
     <fo:page-sequence master-reference=""> 
       <fo:flow flow-name=""> 
         <fo:block></fo:block>
       </fo:flow> 
     </fo:page-sequence> 
 </fo:root>

Remember that the layout-master-set defines the page layout, while the page-sequence applies the layout to specific elements in your source document.

Step 2: Create an XSL-to-FO style sheet

Create a new XSL style sheet (File > New > New Document > XSLT style sheet) .

Add the root level match to the style sheet.

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
        <xsl:template match="/"> 
       </xsl:template> 
    </xsl:stylesheet>

Copy the contents of the fo file into the style sheet inside the <xsl:template> element and make the following changes:

  • Add the xmlns:fo="http://www.w3.org/1999/XSL/Format namespace to the <xsl:templates> element.
  • Specify the master-name attribute of the <fo:simple-master-page> element as main.
  • Specify a region-name attribute for the <fo:region-body> element as body.
  • Specify a master-reference attribute of the <fo:page-sequence> element as main.
  • Specify the flow-name attribute of the <fo:flow> element as body.
  • dd <xsl:apply-templates/> within the <fo:flow> element.

Your resulting file should appear as follows:

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
      <xsl:template match="/"> 
         <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
            <fo:layout-master-set> 
                 <fo:simple-page-master master-name="main"> <fo:region-body region-name="body"/> 
                 </fo:simple-page-master>
           </fo:layout-master-set>
         <fo:page-sequence master-reference="main"> 
            <fo:flow flow-name="body"> 
                <xsl:apply-templates/> 
            </fo:flow>
         </fo:page-sequence> 
      </fo:root> 
      </xsl:template> 
 </xsl:stylesheet>

Step 3 – Add your résumé root element

To test your style sheet, add the following code after the root template:

<xsl:template match="resume"> 
     <fo:block font-family="sans-serif"> 
         <xsl:apply-templates/> 
     </fo:block> 
</xsl:template>

Now configure a new transformation scenario to test your work so far.

Step 4 – Configure oXygen to transform your code to xsl-fo

To configure oXygen to work with your transform, complete the following steps:

  1. With your style sheet open, select Document > Transformation > Configure Transformation Scenario and click New.
  2. From the list, select XSLT transformation.
  3. In the Name field, name your transformation scenario. Ensure that the name is different than your html transform.
  4. On the XSLT tab, click the folder to the left of the XML URL field and browse to and select your yourName-resume.xml instance file.
  5. On the FO Processor tab, select the Perform FO Processing checkbox. Leave the method as PDF and the processor as Apache FOP.
  6. On the Output tab, enter /output/${cfn}.pdf in the Save As field. Doing so will save the pdf output with the same name as the current file {cfn} with an extension of pdf in the output folder.
  7. Select the Open in Browser/System Application checkbox.
  8. Click OK to return to the Configure Transformation Scenario(s) window.
  9. Click Save and Close.

Verify that the content of your resume is appearing in the PDF by selecting Document > Transformation > Apply Transformation or clicking the button on the toolbar.

Step 5 – Add all the elements that make up your résumé

Now it is just a matter of writing a template rule for each of your XML elements in your résumé. The syntax is much like CSS.

For example, to format the <name> element, you would create a template match for <school> and then add an <fo:block> for the name element that specifies the way that you would like to format the name.

    <xsl:template match="school">
        <fo:block font-family="sans-serif" font-size="24pt">
            <xsl:value-of select="name"/> 
        </fo:block> 
    </xsl:template>

Format the remaining elements within your résumé document. Ensure that each section (the personal information, education, work history, and skills sections) is formatted appropriately. A good resource for more samples and examples of the fo styles is the W3 Schools site: http://www.w3schools.com/xslfo/default.asp

Step 6 – Save your style sheet and upload your finished files to Blackboard

Save the XSL file as yourName-resume-fo.xsl, where yourName is your first initial and last name (for example, vmorrow-resume.xsl).

Include the following files in a zipped folder named yourName-resume-xsl.zip:

  • XML résumé (yourName-resume.xml)
  • XML résumé DTD (yourName-resume-DTD.dtd)
  • XSLT-to-FO style sheet (yourName-resume-fo.xsl)

Submit these files through the Exercises section in Blackboard.

Do not include the PDF output. I will do a transformation using your file and base your grade on how well it demonstrates your mastery of the material. Your XSL-FO transformation should be well-formed and successfully transform all the elements of the XML and include them in the result document (PDF).