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:
<?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 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.":
<fo:block>Hello, world.</fo:block>
The completed document should look like this:
<?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 master-name="BasicPage">
<fo:region-body region-name="Body" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="BasicPage">
<fo:flow flow-name="Body">
<fo:block>Hello, world.</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
Next: Processing a document >>
More XML Articles
More By Peyton McCullough