If you need to format content not just for web pages, but for pages that will actually be printed out, keep reading. There is a technology called XSL Formatting Objects that can help you get the job done. In this article, we'll explain how to use it to format your content appropriately.
Using XSL Formatting Objects - Creating a Hello world document (Page 2 of 4 )
Let's get started writing an XSL-FO document, one that simply places the text "Hello, world." on a page. Call the document helloworld.fo (.fo is the typical file extension for XSL-FO). The root element for an XSL-FO document is called root, and the typical namespace prefix for XSL-FO elements is fo. Let's go ahead and create the root element, along with the namespace declaration and the XML declaration:
Next, we need to define a "page master," a type of formatting object which can be thought of as a page template. A page master specifies a page's geometry, such as its size and margins. We'll get into more detail later on. For now, let's just create a minimal page master. All masters are contained within the layout-master-set element:
<fo:layout-master-set>
</fo:layout-master-set>
To create an individual page master, we'll need to create a simple-page-master element. Page masters must be named using the name attribute. Let's create a page master named "BasicPage":
<fo:simple-page-master master-name="BasicPage">
</fo:simple-page-master>
A page can be divided into regions, which contain the content of the page. So, in order to make way for content, we must create a region within the page master. This region must be given a name using the region-name attribute. There are several elements available to create regions. We'll be using the region-body element because we want to place content in the body of the page. Let's create this element, and let's name the region "Body":
<fo:region-body region-name="Body" />
Now we need to add some content. To create a page, we need to create a page-sequence element, which must be a child of the root element. The content contained within a page-sequence element will "flow" from one page to the next as needed. We need to set the master-reference attribute to point to the page master that we just created:
<fo:page-sequence master-reference="BasicPage">
</fo:page-sequence>
To add content to the region we created, we need to use a flow element. The flow-name attribute of this element specifies the region of the page where the content will go. Below, we create a flow element, which goes inside of the page-sequence element:
<fo:flow flow-name="Body">
</fo:flow>
Now we can add a "block" of content using the block element, and inside of the block, we can put "Hello, world.":