XML

A More Complete XSL-FO Example

You really can't appreciate the power of XSL-FO without applying it to a substantial amount of content, consisting of at least a few pages of text. The excerpt I'm using as an example here includes a funny childhood story of my brother along with a clever football ticket caper that he pulled while in high school. Anyway, back to XSL-FO!

Listing 14.2 contains the partial code for the Great Sporting Events sample XSL-FO document, which contains plenty of text to flow across several pages.

Listing 14.2. The Great Sporting Events XSL-FO Document
 1: <?xml version="1.0" encoding="utf-8"?>
 2: <!DOCTYPE fo:root SYSTEM
    "http://xep.xattic.com/xep/resources/validators/dtd/fo.dtd">
 3:
 4: <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 5:   <fo:layout-master-set>
 6:     <fo:simple-page-master master-name="main" page-width="8.5in"
 7:     page-height="11in" margin-left="1in" margin-right="1in"
 8:     margin-top="0.5in" margin-bottom="0.5in">
 9:       <fo:region-body margin-top="0.25in" margin-bottom="0.25in"/>
10:       <fo:region-before extent="0.25in"/>
11:       <fo:region-after extent="0.25in"/>
12:     </fo:simple-page-master>
13:   </fo:layout-master-set>
14:
15:   <fo:page-sequence master-reference="main" initial-page-number="1">
16:     <fo:static-content flow-name="xsl-region-before">
17:       <fo:block text-align="end" font-size="10pt" font-family="serif"
18:       line-height="14pt" >
19:         Great Sporting Events - p. <fo:page-number/>
20:       </fo:block>
21:     </fo:static-content>
22:
23:     <fo:flow flow-name="xsl-region-body">
24:       <fo:block font-size="18pt" font-family="sans-serif" line-height="26pt"
25:       space-after="5pt" background-color="black" color="white"
26:       text-align="center" padding-top="0pt">
27:         Great Sporting Events
28:       </fo:block>
29:
30:       <fo:block font-size="16pt" font-family="sans-serif" line-
          height="20pt"
31:       space-before="10pt" space-after="10pt"
32:       text-align="center" padding-top="0pt">
33:         by Steve
34:       </fo:block>
35:
36:       <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt"
37:       space-after="3pt" text-align="start">
38:         Sports have always seemed to be a large part of my life either as an
39:         athlete participating or as a fan. I was raised in a very sports-
40:         oriented environment. My father was still a young man when I was
            born.
41:         He was very athletic and as a child, I went with him as he played
42:         fast-pitch softball, softball, basketball and sometimes was allowed
43:         to go to the golf course with him. As I recall, all of these
44:         activities were held at public parks and recreation centers. I think
45:         my brother and I got our competitiveness directly from our dad. None
46:         of us are sore losers, but we love to win and hate to lose.
47:       </fo:block>
48:
49:       <fo:block font-size="16pt" font-family="sans-serif" line-height="20pt"
50:       space-before="10pt" space-after="7.5pt"
51:       text-align="start" padding-top="0pt">
52:         Missing Gym Rat
53:       </fo:block>
54:
55:       <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt"
56:       space-after="3pt" text-align="start">
57:         I remember one occasion at the local recreation center where my dad
58:         and his buddies played softball and basketball regularly. On this
59:         particular night, they were playing some hotly contested pick-up
60:         basketball games and I ran all over the rec. center as the night
61:         wore on. I was probably 5 years old and my mom had stayed home that
            night.
62:         This obviously allowed me quite a bit of freedom as my dad played
63:         ball. Near the end of the night, my dad and another player got into
64:         a little scrap. A little fighting was not all that dangerous back in
65:         1970. This was not uncommon and would usually end up with the two
66:         combatants shaking hands and later having a beer together. I
67:         remember being worried as I watched the scuffle. Afterwards,
68:         everyone started playing again so I wandered on off to play in the
69:         center. After what seemed like a long time, I entered the gym and to
70:         my amazement, it was completely empty. I ran to the lobby of the
71:         center and the doors were locked and I saw no cars in the parking
72:         lot. It wasn't very long before the man that worked at the center
73:         unlocked the front doors and my dad was with him.
74:       </fo:block>
75:
76:       ...
77:
78:       <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt"
79:       space-after="3pt" text-align="start">
80:         We returned to the golf course in time to watch the opening kick-off
81:         on T.V. and soon returned all of our &quot;borrowed&quot; items. The
82:         failure of that plan still pains me today.
83:       </fo:block>
84:     </fo:flow>
85:   </fo:page-sequence>
86: </fo:root>

Similar to the skeleton example earlier in the lesson, this document contains a single master page that applies to all of the pages in the document. However, that's pretty much where the similarities between the two examples end. The Great Sporting Events document establishes a header and footer, along with a document body region (lines 9 through 11). The page number is displayed in the header along with the name of the document, as is evident in lines 16 through 21 where a static block is created. Notice that the initial page number is established in the fo:page-sequence element via the initial-page-number attribute (line 15).

With the header nailed down, the flowed content then begins with the title of the document, which is created as a block with large white centered text against a black background (lines 24 to 28). The author of the document, Steve, is then displayed in the next block in smaller centered text (lines 30 to 34). From there, the bulk of document content begins with block after block of story text.

There are two subheadings worth pointing out, one of which isn't visible in the listing. The subheading that is visible appears as a block on lines 49 through 53, and simply breaks up the text into a sub-story with the heading Missing Gym Rat. One other thing to note is how standard XML conventions are used in terms of referencing quotes as entities (&quot;), as is visible on line 81.

I encourage you to study the Great Sporting Events XSL-FO code to make sure that you understand fully how it works. When you're satisfied that you follow what's going on, you're ready to run the document through FOP to generate a PDF file via the following command:

fop sportsstory.fo sportsstory.pdf

The resulting PDF document is shown in Figure 14.5 as viewed in Adobe Acrobat Reader.

Figure 14.5. The Great Sporting Events PDF document as viewed in Adobe Acrobat Reader.

The XSL-FO styles that you carefully analyzed in Listing 14.2 are realized in the PDF document shown in the figure. The resulting PDF document is actually three pages, which means you can use Acrobat Reader's forward and back page navigation features to view each page in the document.

by BrainBellupdated
Advertisement: