January 10, 2009

Page layout in XSLT

Filed under: Uncategorized — John Walkup @ 9:19 pm

In many ways, XSL uses a fairly straightforward method for defining the manner in which text is “flowed” into a page. In other ways, I find it positively goofy (but more on that later).

Consider the following XSLT style sheet. I have dispensed with the usual XSLT boiler plate for brevity, so this snippet begins with the <xsl:template match=”table”> declaration (although that alone is a sore point with me, but more on that later.)

Also, I have not printed the contents of the corresponding XML page either, although I will mention that it contains a node element called <date>. At this point, we are not concerned about what is being printed, but rather how it is being processed. Note that there is one layout master defined (XSL won’t allow multiple layout masters), but two page masters.

<xsl:template match=”sales”>
<fo:root>
<!– layout-master (page setup) –>

<fo:layout-master-set>
<fo:simple-page-master master-name=”body”>
<fo:region-body region-name=”portrait” margin-top=”9pt”/>
</fo:simple-page-master>

<fo:simple-page-master master-name=”appendix”>
<fo:region-body region-name=”landscape” margin-top=”5pt”/>
</fo:simple-page-master>
</fo:layout-master-set>

<!– page sequence (containts page content) –>

<fo:page-sequence master-reference=”body”>
<fo:flow flow-name=”portrait”>
<fo:block>
<xsl:apply-templates select=”truck”/>
</fo:block>
</fo:flow>
</fo:page-sequence>

<fo:page-sequence master-reference=”appendix”>
<fo:flow flow-name=”landscape”>
<fo:block>
<xsl:apply-templates select=”date”/>
</fo:block>
</fo:flow>
</fo:page-sequence>

</fo:root>
</xsl:template>

I called one page master “body” (that is, the main body of the paper) and the other “appendix,” which correspond to two sections of a large-scale document that could require their own special treatment. I also called one region body “portrait” and the other “landscape,” although I am not sure why anyone would want an appendix typeset in landscape mode. Note that I have not declared anything in the “landscape” region body that would typeset it in landscape form, at least not yet.

From what I can tell so far, declaring a page master is similar to selecting certain attributes in the File/Page Setup command in Microsoft Word, or creating text boxes within Adobe InDesign. (I don’t think LaTeX has a similar functionality in this regard.)

The result of this transformation is to create two pages, with the contents of the <truck> element of the XML file spilling into the “body” page master on the first page and the “appendix” page master on the second page.

Now, here is what I don’t fully understand: Since page masters cannot contain more than one region body, then what is the purpose of naming the region body? Would the name of the page master not suffice since it has to be unique within the XSL template? In the examples I have seen in books, authors are ascribing the same name to the region body as to the page master, which seems only natural, but also superfluous.

Since layout masters can contain more than one page master, then why do authors avoid introducing this added sophistication in their books at the time the concept of the page master is being developed? To me, showing a layout master with two page masters, with each page master having a unique

Gripe of the day

I realize that computer book authors like to show off bells and whistles, but too much of it can get out of hand. For example, in the above code I only showed one change in the style format for each region body. Sure, I could have set the left margins, right margins, bottom margins, and so on, but what would that teach? I suggest showing one bell or whistle, then referring the reader to the others. This keeps the code simple and saves space.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress