Home arrow XML arrow Page 4 - Using Regions with XSL Formatting Objects

Using Regions with XSL Formatting Objects

In my last article on XSL Formatting Objects (XSL-FO), we took a look at the basics. We created a simple page master with one body region and then wrote some text onto the page. However, XSL-FO offers a lot more functionality and flexibility. In this article, we'll take a closer look at regions.

Author Info:
By: Peyton McCullough
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
June 02, 2009
  1. · Using Regions with XSL Formatting Objects
  2. · Defining Additional Regions
  3. · Placing Content Within Regions
  4. · A Short Example

print this article

Using Regions with XSL Formatting Objects - A Short Example
(Page 4 of 4 )


Throughout the article, I've been discussing how regions might be used in designing a page out of a book. Let's apply a bit of what we've gone over to create a short and very simple example, so that you can see regions in action. We'll go ahead and create the page that we've been talking about so that you can see how the finished product would look. Plus, there's also one additional point that I've left out so far, but I'd rather show it to you than merely tell you about it. 

The page will be pretty standard, but the idea here is to create a short example illustrating how the pieces fit together. On the top of the page will be the name of the book, and on the bottom of the page will be the page number. In the center of the page will be, of course, the text of the page. 

We'll start from the very beginning. First, we need to provide an XML declaration and a root element: 

<?xml version="1.0" encoding="UTF-8"?><fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"></fo:root>

Next, we need to define the master page. We'll assign the name "BookPage" to the master page. In order to achieve the desired look, we'll have to use three regions: the body region, the top region and the bottom region. Remember that the body region must be defined first. The default region names will do fine. 

All that needs to be done now is to actually create a page or pages using the page master. To populate the top and bottom regions, the static-content element is used, and to populate the body region, the flow element is used. Remember that any static-content elements must go before the flow element. 

<fo:layout-master-set> <fo:simple-page-master master-name="BookPage"page-width="110mm" page-height="178mm" margin="10mm"> <fo:region-body /> <fo:region-before /> <fo:region-after /> </fo:simple-page-master></fo:layout-master-set>

In the top region, we'll just put the name of the book. Often, a book will alternate what's at the top of the page. One page will have the name of the book, and then the opposite page will have the name of the chapter, or the author's name, or some additional piece of information like that. While it's possible to do this with XSL-FO, some additional functionality would be necessary, and that would take us into some entirely different topics. So, for this example, the name of the book will appear on every page. 

In the bottom region, the page number will appear. This is done using the page-number element, which is very simple and doesn't need any sort of additional explanation. Finally, in the body region, the content will appear. I'll just put a short sentence, because I don't want to take up too much space, but you'll probably want to think up some more content (or do some copying and pasting) to make the example more complete. 

Here's what all of that looks like: 

<fo:page-sequence master-reference="BookPage"> <fo:static-content flow-name="xsl-region-before"> <fo:block>Using XSL</fo:block> </fo:static-content> <fo:static-content flow-name="xsl-region-after"> <fo:block> <fo:page-number /> </fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <fo:block>This is a short example. Replace this with whatever block of text you want in order to make it longer.</fo:block> </fo:flow></fo:page-sequence>

You might want to replace the body text with something longer to get an idea of what the page will look like filled with content, but in any case, the document is now ready to be put through the processor. Process it using whatever options you like. The easiest way would probably be just to use fop's built-in viewer to see the result: 

$ fop examplePage.fo -awt

Immediately, you should notice something very wrong. The content in the top and bottom regions actually overlaps with the body content. This is because all regions aren't created equally. The non-body regions are, in a way, part of the body region. In order to separate them, margins must be defined for the body region. This is easy to do. Simply replace the region definitions in the original XSL-FO file with the following definitions: 

<fo:region-body margin-top="30mm" margin-bottom="30mm" /><fo:region-before extent="30mm" /><fo:region-after extent="30mm" />

As you can see, we simply added two attributes to the region-body element. The first is margin-top, which specifies a length for the top margin, and the second is margin-bottom, which specifies a length for the bottom margin. That much is pretty self-explanatory. Then, we added an extent attribute to each of the non-body region definitions. This simply specifies the extent of the region. Without these attributes, you'll get an error when you try to process the page. 

The output should now look much better, with each bit of text in its proper place rather than all overlapping each other. Of course, there are a number of improvements that could be made. For example, the title and page number could both be centered, and the content of the top region could be alternated with the pages, as was discussed earlier. However, since those those improvements must be made outside of the functionality provided by regions, and are thus outside the scope of this article, I'll leave them to you. 

Now you should have a basic understanding of regions in XSL-FO. Go out and try to create some more documents with regions. Experiment a bit to see just what you can create with regions.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials