Chapter 10. Putting It All Together

CONTENTS
  •  10.1 Outline

A small, complete document uses much of the previously described functionality and illustrates the power of XSL-FO.

10.1 Outline

As source content, I'm going to use an imaginary book outline. In terms of layout, the major elements are the front matter, which requires attention and is a one-off; the main chapters, which share layout but have one or two special treatment areas; and the rear matter, for which separate treatment is needed for an index.

The pagination needed is shown in Figures Figure 10-1 and Figure 10-2.

Figure 10-1. Frontmatter layout

figs/xslf_1001.gif

Figure 10-2. Initial page layout

figs/xslf_1002.gif

No page headers are needed for the first three pages, page numbers are needed only for the preface and table of contents, then we use a header (no footer), as shown, for the remainder of the chapters. Now we can specify these and define some of the attribute sets that will be used throughout.

So let's put an outline together. I'm presuming a fairly complex stylesheet, so I'll partition it early. The main stylesheet will hold only the content templates and will import separate stylesheets for the page layout (layout master set), the primary element templates. I'll start with a trial for the layout master set.

First, separate simple-page-masters will be needed for the first three pages that share a common layout. Others will be needed for the preface and contents pages and another for the bulk body content. I'll prepare those first and try them out.

Rather than include the first layout inline, it is a separate page with comments. Now we can convert it to something that is usable with content. Let's have a quick look at the format I'm using for the book itself. This is shown in outline in Appendix F. The next task is to convert the outline into a stylesheet that matches this schema. First, the root template is placed around what was an XML document in the fo namespace, then templates are applied, or called, to replace the dummy content.

So far, there are three files. The page layout stylesheet is derived directly from the test layout, pl.xsl (page layout). Another stylesheet is created with the majority of the templates, main.xsl . A third is created to hold all the standard property sets to be used, ps.xsl (property sets). The main stylesheet is the second of these; the other two are imported from that.

10.1.1 Page Layout

The page layout stylesheet is the key to the whole layout process, and it is important to see how the application of templates relates to this file. The primary flows are in the title page, the front matter for the preface, table of contents, and finally, the body matter, which picks up both chapter content and rear matter. Within each page-sequence, the right apply-templates statements are needed.

For the page-sequence named first3, the flow content is as shown in Example 10-1.

Example 10-1. Flow contents for the first three pages
<fo:page-sequence master-reference="first3">   <fo:flow flow-name="xsl-region-body">     <xsl:apply-templates select="/book/frontmatter/title"/>     <xsl:apply-templates select="/book/frontmatter/dedication"/>     <xsl:call-template name="tPage"/>   </fo:flow> </fo:page-sequence>

Remember that, prior to this point in the stylesheet, all content is a literal result element. All you need is to apply selected templates for the needed content and to ensure that other templates don't duplicate the content elsewhere. Hence the use of the select attribute on apply-templates. This takes care of the first three pages, or it will when templates are provided to match this content.

The next flow area includes the preface and table of contents. This is shown in Example 10-2.

Example 10-2. Preface and table of contents flow
<fo:page-sequence    master-reference="prefAndToc" [1]initial-page-label="1" [2]format="I"> [3]      <fo:static-content                   flow-name="xsl-region-after">      <fo:block text-align="outer"><fo:page-number/></fo:block>    </fo:static-content>    <fo:flow flow-name="xsl-region-body">    <xsl:apply-templates select="/book/frontmatter/preface"/>    <xsl:apply-templates select="/" mode="toc"/>    </fo:flow>  </fo:page-sequence>
  1. The page numbering is reset to 1.

  2. The format is specified as Roman uppercase.

  3. The region-after contains the page number only.

The only content needed here is the preface and table of contents. Again, a full path to each is provided. The toc needs the full document to access each section, but is moded to ensure proper processing. Only the footer is used here; there is no header. The page number is reset to 1, and its format is set to Roman numbering.

Finally, the main body content is applied to the remaining page-sequence, as shown in Example 10-3.

Example 10-3. Chapter page-sequence
 <fo:page-sequence        master-reference="chaps"           initial-page-label="1" [1]       format="1">     <fo:static-content       flow-name="xsl-region-before"> [2]  <fo:block text-align="outside">        Chapter    <fo:retrieve-marker [3]       retrieve-class-name="chapNum"/>     <fo:leader leader-pattern="space" />        <fo:retrieve-marker [4]       retrieve-class-name="chap"/>     <fo:leader leader-pattern="space" /> [5]  Page  <fo:page-number font-style="normal" /> [6]  of  <fo:page-number-citation ref-id='end'/>   </fo:block>     </fo:static-content>     <fo:flow flow-name="xsl-region-body">     <fo:block  xsl:use-attribute-sets='font'>     <xsl:apply-templates select="/book/bodymatter"/>     <xsl:apply-templates select="/book/rearmatter"/>   </fo:flow> </fo:page-sequence>
  1. The page number is formatted in Roman.

  2. This block forms the header on these pages.

  3. The chapter number is retrieved as a marker.

  4. The chapter title is retrieved as a marker.

  5. The page number is added.

  6. Last page number of document.

Markers are used for the static content in the header, both for the chapter title and the chapter number. The last page is identified by a block with an id value of end, whose page number is retrieved here.

10.1.2 The Template File

The template file is where the stylesheet author will spend most of his or her time. I like to keep mine as uncluttered as possible. I've laid it out almost in document order, so that if I'm looking for a template that matches front matter, I'll expect it near the top of the file. I keep all general templates near the end of the file, finishing up with the default template. This is what works for me.

I almost always write a default template first, which helps to identify missing templates. It reads as in Example 10-4.

Example 10-4. The default template
<xsl:template match="*"> [1] <fo:block color="red">     Element <xsl:value-of             select="name(..)"/>/ <xsl:value-of             select="name(  )"/> found, with no template.     </fo:block>  </xsl:template>
  1. A block with red content.

All this tells me is the parent and element in question by pointing them out in red. Note that there is no child processing, hence, it catches the parent prior to a child. This way, I know I need to keep writing templates until I have no more red text displayed.

Within this file, I want to keep separate the actual style (appearance) of the various blocks and inlines. I separate these out, for the most part, by means of attribute sets, in the ps.xsl file (I will define page breaks within this file). One approach is to start at the front and, for each element of the source document, work through until the formatting has been achieved. Prior to this, I want to set up some property sets for the major items, such that they are available for use when this file is worked on. This prevents a random approach, which results in styles being spread all over the file. As I consider each separate style, I want to review it as a candidate for either using an existing style or for forming the basis for another style.

Let's start with the title page. This needs to be on a separate page with sufficient spacing. The content is obtained from the source content by using the pull method. Because it's a one-off, all styling will be applied directly within the template. This requires something such as what's shown in Example 10-5.

Example 10-5. Template for the front page
 <xsl:template name="tPage">  <fo:block  xsl:use-attribute-sets='font'>    <fo:block      font-size='36pt'      space-before = '50mm'      space-after =  '25mm'      ><xsl:value-of select='/book/title'/>  </fo:block>  <fo:block    font-size='18pt'    space-before = '25mm'    space-after =  '12mm' >by </fo:block>  <fo:block    font-size='18pt'    space-before = '25mm'    space-after =  '12mm'>    <xsl:value-of select='/book/frontmatter/author'/>  </fo:block>     </fo:block>     <fo:block text-align='end'        font-size='10pt'        space-before = '50mm'>      &#x00A9; <xsl:value-of          select="/book/frontmatter/pubdetails/pubname"/>     </fo:block>       <fo:block text-align='end'        font-size='10pt'>   <xsl:value-of          select="/book/frontmatter/pubdetails/pubads"/>       </fo:block>   </xsl:template>

Next, for the dedication page. Literal text is needed here for a title, but otherwise, this is a simple, separate page, styled in the same manner as the title page. The contents of the dedication can be handled by the standard paragraph template.

Next comes the fancy title page. This sits between the dedication and the preface, hence, it needs to be inserted manually, again using the pull technique. A template using XSLT modes inserts the content at the right time in the output. I have to force the styling to obtain the side-by-side appearance of the image and the publisher data because I don't have the appropriate float properties available in my formatter. I'm going to use a table containing the image and the text, as shown in Example 10-6.

Example 10-6. Fancy title page template
<xsl:template match='/' mode='ffp'>    <fo:block break-before="odd-page" >      <fo:block        xsl:use-attribute-sets="title font"        space-after="20mm"> [1]    <xsl:value-of select='/book/title'/>      </fo:block>      <fo:table width='130mm'>        <fo:table-body>          <fo:table-row>            <fo:table-cell>              <fo:block>              <fo:external-graphic                    src="images\ttlpg.jpg" [2]                content-height="100%"                    scaling="uniform"/>            </fo:block>            </fo:table-cell>            <fo:table-cell display-align='bottom'>              <fo:block/>              <fo:block space-before='90mm'> [3]              <xsl:value-of               select='/book/frontmatter/pubdetails/pubname'/>              </fo:block>              <fo:block>                 <xsl:value-of                  select='/book/frontmatter/pubdetails/pubads'/>              </fo:block>            </fo:table-cell>          </fo:table-row>        </fo:table-body>      </fo:table>      <fo:block   font-size='{$small-sz}'>Image courtesy of Aries Cheung, Toronto   </fo:block>    </fo:block>  </xsl:template>
  1. Title

  2. Image

  3. Text content

With the new page layout for the preface and contents, the styling for these pages can be applied. The preface is styled the same as the body of the book, so standard templates can be used. The table of contents requires the moded template.

Because we don't have ID values in each chapter, appendix, etc., we need to generate them to obtain the page number. This means that for the preface, each chapter, and each appendix, the template must generate the id value. Within the table of contents, it is necessary to change context to generate an id value. The template for the table of contents is shown in Example 10-7.

Example 10-7. Table of contents template
<xsl:template match="/" mode="toc">  <fo:block break-before="odd-page" >    <fo:block xsl:use-attribute-sets="title font"> [1]  Table of Contents    </fo:block>    <xsl:for-each select='book/frontmatter/preface'>     <fo:block text-align-last="justify">      <fo:inline><xsl:value-of select="title"/>       <fo:leader        leader-pattern="dots"/> [2]   <fo:page-number-citation ref-id="{generate-id      </fo:inline>     </fo:block>    </xsl:for-each> [3]<xsl:for-each select='book/bodymatter/chapter'>     <fo:block text-align-last="justify">      <fo:inline><xsl:value-of select="title"/>       <fo:leader        leader-pattern="dots"/>       <fo:page-number-citation ref-id="{generate-id(  )}"/>      </fo:inline>     </fo:block>    </xsl:for-each> [4]<xsl:for-each select='book/rearmatter/appendix'>     <fo:block text-align-last="justify">      <fo:inline><xsl:value-of select="title"/>       <fo:leader        leader-pattern="dots"/>       <fo:page-number-citation ref-id="{generate-id(  )}"/>     </fo:inline>    </fo:block>   </xsl:for-each>  </fo:block> </xsl:template>
  1. Table of contents title

  2. Preface

  3. Chapters

  4. Appendixes

This uses the standard table of contents methods discussed earlier in the book.

The remainder of the book's formatting is fairly straightforward, using already defined property sets. If the early definitions are right, there needs to be less special formatting as the stylesheet grows. Chapters repeat the preface format, and most of the basic formatting is now available.

10.1.3 Property Sets

This file holds base variables, attribute sets, and little else. It is included by the main.xsl file. The ps.xsl file is shown in Example F-4.

A quick glance at the source documents indicates some ready candidates for repeated styling:

  • Chapter titles

  • The basic paragraph element

Let's start with the basics for these. First, begin with the font and some base sizes that I can add to as needed. The intent is never to specify an absolute size in the main.xsl file.

The first four variables set out a base size and three variants on that size, each a multiple of the base font. To call these sizes up, next come strings used as the attribute value font-size. The font attribute set, which comes next, defines the base font for the whole document. This is called up frequently throughout the document. A lengthy attribute-set follows for the title formatting. This, in turn, uses previously defined variables. I've specified a padding attribute set, though it hasn't been used so far. The same goes for the border attribute set. Finally, the paragraph attribute set is specified. This file can be expanded to contain all the actual styling details, which are then called up from the main stylesheet.

This chapter has shown a general approach that may be used to create a complete stylesheet. For reference, all the files are included in Example F-4: the outline source document is in Example F-2, the main stylesheet is in Example F-3, the property sets file is in Example F-4, and the page layout file is in Example F-5.

CONTENTS


XSL-FO
Xsl Fo
ISBN: 0596003552
EAN: 2147483647
Year: 2002
Pages: 24
Authors: Dave Pawson

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net