| Previous | Next
Laying Out the Master PagesXSL-FO 1.0 only defines one kind of master page, the <fo:simple-page-master margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="first"> <!-- Separate parts of the page go here --> </fo:simple-page-master> The part of the page inside the margins is divided into five regions: the start region, the end region, the before region, the after region, and the body region. Where these fall on a page depends on the writing direction. In left-to-right, top-to-bottom languages like English, start is on the lefthand side, end is on the righthand side, before is on top, and after is on bottom as diagramed in Figure 13-2. However, if the text were Hebrew, then the start region would be on the righthand side of the page, and the end region would be on the lefthand side of the page. If the text were traditional Chinese, then the start would be on top, the end on bottom, the before on the lefthand side, and the after on the righthand side. Other combinations are possible. Figure 13-2. The five regions in a left-to-right, top-to-bottom writing systemThese regions are represented by The body region and the corresponding <fo:simple-page-master margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="first"> <fo:region-body/> </fo:simple-page-master> However, you can add <fo:simple-page-master margin-right="0.5in" margin-left="0.5in" margin-bottom="0.5in" margin-top="0.5in" page-width="8.5in" page-height="11in" master-name="first"> <fo:region-before extent="0.5in"/> <fo:region-after extent="0.5in"/> <fo:region-start extent="0.5in"/> <fo:region-end extent="0.5in"/> <fo:region-body margin-top="1.0in" margin-bottom="1.0in" margin-left="0.5in" margin-right="0.5in"/> </fo:simple-page-master> Most of the time, the details of the layout-master set are fixed in the stylesheet. For example, here's the revised XSLT template that includes a full <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="first"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="first"> <!-- data to place on the page --> </fo:page-sequence> </fo:root> </xsl:template> Flowing Content into the PagesNext we add a The formatter instantiates a page based on the master page named by the The <fo:flow flow-name="xsl-region-body"> <fo:block>Southern Corn Bread</fo:block> <fo:block>1 cup flour</fo:block> <fo:block>4 tablespoons Royal Baking Powder</fo:block> <fo:block>/ teaspoon salt</fo:block> <fo:block>1 cup corn meal</fo:block> <fo:block>1/ cups whole milk</fo:block> <fo:block>4 tablespoons melted butter</fo:block> <fo:block> Sift flour, baking powder, sugar & salt together. Add 1 cup corn meal. Beat egg in cup and add beaten egg and 1/ cups whole milk to make a batter. Stir well. Add melted shortening and beat until light and thoroughly mixed. Pour into greased shallow pan or greased muffin rings. Bake in hot oven at 425º F for 25 minutes. Cut into squares if cooked in shallow pan. </fo:block> <fo:block> After my mother-in-law Marjorie Anderson died, Beth and I found this recipe written on the "extra recipes" page in a local tutorial in her cupboard. This was published by the The Episcopal Churchwomen, Church of Ascension, Mt. Sterling, Kentucky. </fo:block> </fo:flow> Here's an XSLT template that produces the content of this <xsl:template match="dish|ingredient|directions|story"> <fo:block><xsl:apply-templates/></fo:block> </xsl:template> Generating the Finished DocumentWe now have the minimum set of pieces needed to put together a full XSL-FO document. Example 13-2 is an XSLT stylesheet that transforms documents like Example 13-1 into XSL formatting objects documents. Example 13-2. An XSLT to XSL-FO transform<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="first"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="first"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="dish|ingredient|directions|story"> <fo:block><xsl:apply-templates/></fo:block> </xsl:template> </xsl:stylesheet> Example 13-3 shows the complete XSL-FO document produced by running the cornbread recipe through an XSLT engine such as Xalan or SAXON with this stylesheet. The whitespace is a little off because of the way XSLT treats whitespace in the transform document. However, this won't be significant when the document is rendered. Example 13-3. An XSL-FO document describing a recipe for cornbread<?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 margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="first"><fo:region-body/></fo:simple-page-master> </fo:layout-master-set><fo:page-sequence master-reference="first"> <fo:flow flow-name="xsl-region-body"> <fo:block>Southern Corn Bread</fo:block> <fo:block> 1 cup flour </fo:block> <fo:block> 4 tablespoons Royal Baking Powder </fo:block> <fo:block> / teaspoon salt </fo:block> <fo:block> 1 cup corn meal </fo:block> <fo:block> 1/ cups whole milk </fo:block> <fo:block> 4 tablespoons melted butter </fo:block> <fo:block> Sift flour, baking powder, sugar & salt together. Add 1 cup corn meal. Beat egg in cup and add beaten egg and 1/ cups whole milk to make a batter. Stir well. Add melted shortening and beat until light and thoroughly mixed. Pour into greased shallow pan or greased muffin rings. Bake in hot oven at 425º F for 25 minutes. Cut into squares if cooked in shallow pan. </fo:block> <fo:block> After my mother-in-law Marjorie Anderson died, Beth and I found this recipe written on the "extra recipes" page in a local tutorial in her cupboard. This was published by the The Episcopal Churchwomen, Church of Ascension, Mt. Sterling, Kentucky. </fo:block> </fo:flow></fo:page-sequence></fo:root> The final step in this process is to convert the formatting objects document into some other format that can be viewed onscreen or on paper. This requires running a formatting program such as the Apache XML Project's open source FOP (http://xml.apache.org/fop/). FOP is a Java program that runs on most platforms. At the time of this writing, it has some significant holes in its coverage but is making rapid progress. After you install FOP in the class path, this command line transforms the file cornbread.fo into a PDF document: % FOP can also transform XSL-FO documents into plain text, raw PostScript, a PCL file, or SVG slides, or display it on the screen using the Java 2D API. This command produces the window shown in Figure 13-3. Figure 13-3. The XSL-FO recipe document in FOP's AWT previewThere are several other programs for working with XSL-FO documents:
|