In order to process an XSL stylesheet, a stylesheet processor accepts data in XML and an XSL stylesheet to define the presentation of that XML. But there are two parts of the presentation:
- Construction of a final document, called a results tree - defining how the document will be used, for instance in print, on the Web, on a handheld device, and so on
- Interpretation of that results tree to produce formatted results - defining the look and feel of the document
The results tree is generated by XSLT, and is outside the scope of this article. This article is going to talk about XSL:FO - the formatting objects that allow you to interpret your XML and produce formatted results.
Look at an XSL:FO Document
(Note: line numbers included for reference, they are not a part of the document.)
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
4 <fo:simple-page-master master-name="basic_page">
5 <fo:region-body margin="1in"/>
8 <fo:page-sequence master-reference="basic_page">
9 <fo:flow flow-name="xsl-region-body">
10 <fo:block font-family="Arial" font-size="14pt"> 11 Hello, world! 12 </fo:block>
Lines 1 and 2 declare that this is an XML document. Line 1 is the XML declaration and line 2 is the root element with the namespace listed. Line 3 is the wrapper around all masters used in the document. I'm using a simple-page-master (line4) to define the geometry of the page and give it a name. My basic_page will have a margin of 1 inch on all sides.
Once you've defined the master pages, you can start defining the sequences of your pages and how they will look. Since I only have one page in my sample document, I only need to define the sequence of one page (line 8). First you enclose all text in a flow element. Since I only defined one flow in my master, that's the one I should use - "xsl-region-body" (line 9). Then the fun begins.
I place all my text inside block elements. Text cannot be placed into a flow without an enclosing element. I'm using the font-family and font-size attributes of the block element to format my text.
What to Do with an XSL:FO Document
Once you have written an XSL:FO document, what can you do with it? Well, you could open it in a Web browser, but it would just display as XML. Instead, you need to get an XSL formatter and convert your XSL document into a formatted masterpiece.