Home arrow XML arrow Page 2 - Using XSL Formatting Objects
XML

Using XSL Formatting Objects


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.

Author Info:
By: Peyton McCullough
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
February 04, 2009
TABLE OF CONTENTS:
  1. · Using XSL Formatting Objects
  2. · Creating a Hello world document
  3. · Processing a document
  4. · Page masters

print this article
SEARCH DEVARTICLES

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>



blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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