XSL-FO TUTORIAL Introduction to XSL-FO XSL-FO is about formatting XML data for output. What You Should Already Know Before you study XSL-FO you should have a basic understanding of XML and XML Namespaces. What is XSL-FO? • XSL-FO is a language for formatting XML data • XSL-FO stands for Extensible Stylesheet Language Formatting Objects • XSL-FO is a W3C Recommendation • XSL-FO is now formally named XSL • XSL-FO is About Formatting • XSL-FO is an XML-based markup language describing the formatting of XML data for output to screen, paper or other media. • XSL-FO is Formally Named XSL Why this confusion? Is XSL-FO and XSL the same thing? Yes it is, but we will give you an explanation: Styling is both about transforming and formatting information. When the World Wide Web Consortium (W3C) made their first XSL Working Draft, it contained the language syntax for both transforming and formatting XML documents. Later, the XSL Working Group at W3C split the original draft into separate Recommendations: • XSLT, a language for transforming XML documents • XSL or XSL-FO, a language for formatting XML documents • XPath, a language for navigating through elements and attributes in XML documents The rest of this tutorial is about formatting XML documents: XSL-FO, also called XSL.
• •
XSL-FO is a Web Standard XSL-FO became a W3C Recommendation 15. October 2001. Formally named XSL.
XSL-FO Documents XSL-FO documents are XML files with output information. XSL-FO Documents XSL-FO documents are XML files with output information. They contain information about the output layout and output contents. XSL-FO documents are stored in files with a .fo or a .fob file extension. It is also quite common to see XSLFO documents stored with an .xml extension, because this makes them more accessible to XML editors. XSL-FO Document Structure XSL-FO documents have a structure like this: Structure explained XSL-FO documents are XML documents, and must always start with an XML declaration:
1
The element is the root element of XSL-FO documents. The root element also declares the namespace for XSL-FO: The element contains one or more page templates: Each element contains a single page template. Each template must have a unique name (master-name): One or more elements describe the page contents. The master-reference attribute refers to the simple-page-master template with the same name: Note: The master-reference "A4" does not actually describe a predefined page format. It is just a name. You can use any name like "MyPage", "MyTemplate", etc.
XSL-FO Areas XSL-FO uses rectangular boxes (areas) to display output. XSL-FO Areas The XSL formatting model defines a number of rectangular areas (boxes) to display output. All output (text, pictures, etc.) will be formatted into these boxes and then displayed or printed to a target media. We will take a closer look at the following areas: • Pages • Regions • Block areas • Line areas • Inline areas • XSL-FO Pages XSL-FO output is formatted into pages. Printed output will normally go into many separate pages. Browser output will often go into one long page. XSL-FO Pages contain Regions. XSL-FO Regions Each XSL-FO Page contains a number of Regions: • region-body (the body of the page) • region-before (the header of the page) • region-after (the footer of the page) • region-start (the left sidebar) • region-end (the right sidebar) XSL-FO Regions contain Block areas. XSL-FO Block Areas •
XSL-FO Block areas define small block elements (the ones that normally starts with a new line) like paragraphs, tables and lists.
2
• XSL-FO Block areas can contain other Block areas, but most often they contain Line areas. XSL-FO Line Areas • XSL-FO Line areas define text lines inside Block areas. • XSL-FO Line areas contain Inline areas. XSL-FO Inline Areas XSL-FO Inline areas define text inside Lines (bullets, single character, graphics, and more).
XSL-FO Output • •
XSL-FO defines output inside elements. XSL-FO Page, Flow, and Block
"Blocks" of content "Flows" into "Pages" and then to the output media. XSL-FO output is normally nested inside elements, nested inside elements, nested inside elements: XSL-FO Example It is time to look at a real XSL-FO example: Hello W3Schools Hello W3Schools Hello
W3Schools
XSL-FO Flow XSL-FO pages are filled with data from elements. XSL-FO Page Sequences
• • • •
XSL-FO uses elements to define output pages. Each output page refers to a page master which defines the layout. Each output page has a element defining the output. Each output page is printed (or displayed) in sequence.
XSL-FO Flow XSL-FO pages are filled with content from the element.
3
The element contains all the elements to be printed to the page. When the page is full, the same page master will be used over (and over) again until all the text is printed. Where To Flow? The element has a "flow-name" attribute. The value of the flow-name attribute defines where the content of the element will go. The legal values are: • xsl-region-body (into the region-body) • xsl-region-before (into the region-before) • xsl-region-after (into the region-after) • xsl-region-start (into the region-start) • xsl-region-end (into the region-end)
XSL-FO Pages XSL-FO uses page templates called "Page Masters" to define the layout of pages. XSL-FO Page Templates XSL-FO uses page templates called "Page Masters" to define the layout of pages. Each template must have a unique name: In the example above, three elements, define three different templates. Each template (page-master) has a different name. The first template is called "intro". It could be used as a template for introduction pages. The second and third templates are called "left" and "right". They could be used as templates for even and odd page numbers. XSL-FO Page Size XSL-FO uses the following attributes to define the size of a page: • page-width defines the width of a page • page-height defines the height of a page XSL-FO Page Margins XSL-FO uses the following attributes to define the margins of a page: • margin-top defines the top margin • margin-bottom defines the bottom margin • margin-left defines the left margin • margin-right defines the right margin • margin defines all four margins XSL-FO Page Regions XSL-FO uses the following elements to define the regions of a page: • region-body defines the body region • region-before defines the top region (header) • region-after defines the bottom region (footer) • region-start defines the left region (left sidebar) • region-end defines the right region (right sidebar) Note that the region-before, region-after, region-start, and region-end is a part of the body region. To avoid text in the body region to overwrite text in these regions, the body region must have margins at least the size of these regions.
4
XSL-FO Example This is an extract from an XSL-FO document: The code above defines a "Simple Page Master Template" with the name "A4". The width of the page is 297 millimeters and the height is 210 millimeters. The top, bottom, left, and right margins of the page are all 1 centimeter. The body has a 3 centimeter margin (on all sides). The before, after, start, and end regions (of the body) are all 2 centimeters. The width of the body in the example above can be calculated by subtracting the left and right margins and the region-body margins from the width of the page itself: 297mm - (2 x 1cm) - (2 x 3cm) = 297mm - 20mm - 60mm = 217mm. Note that the regions (region-start and region-end) are not a part of the calculation. As described earlier, these regions are parts of the body.
XSL-FO Blocks • XSL-FO output goes into blocks. • XSL-FO Pages, Flow, and Block "Blocks" of content "Flow" into "Pages" of the output media. XSL-FO output is normally nested inside elements, nested inside elements, nested inside elements: Block Area Attributes Blocks are sequences of output in rectangular boxes: This block of output will have a one millimeter border around it. Since block areas are rectangular boxes, they share many common area properties: space before and space after • margin • border • padding The space before and space after is the empty space separating the block from the other blocks. The margin is the empty area on the outside of the block. The border is the rectangle drawn around the external edge of the area. It can have different widths on all four sides. It can also be filled with different colors and background images. The padding is the area between the border and the content area. The content area contains the actual content like text, pictures, graphics, or whatever.
5
Block Margin • margin • margin-top • margin-bottom • margin-left • margin-right • Block Border Border style attributes: • border-style • border-before-style • border-after-style • border-start-style • border-end-style • border-top-style (same as border-before) • border-bottom-style (same as border-after) • border-left-style (same as border-start) • border-right-style (same as border-end) Border color attributes: • border-color • border-before-color • border-after-color • border-start-color • border-end-color • border-top-color (same as border-before) • border-bottom-color (same as border-after) • border-left-color (same as border-start) • border-right-color (same as border-end) Border width attributes: • border-width • border-before-width • border-after-width • border-start-width • border-end-width • border-top-width (same as border-before) • border-bottom-width (same as border-after) • border-left-width (same as border-start) • border-right-width (same as border-end) Block Padding • padding • padding-before • padding-after • padding-start • padding-end • padding-top (same as padding-before) • padding-bottom (same as padding-after) • padding-left (same as padding-start) • padding-right (same as padding-end) Block Background • background-color • background-image • background-repeat • background-attachment (scroll or fixed) Block Styling Attributes
6
Blocks are sequences of output that can be styled individually: This block of output will be written in a 12pt sans-serif font. Font attributes: • font-family • font-weight • font-style • font-size • font-variant Text attributes: • text-align • text-align-last • text-indent • start-indent • end-indent • wrap-option (defines word wrap) • break-before (defines page breaks) • break-after (defines page breaks) • reference-orientation (defines text rotation in 90" increments) Example W3Schools At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. Result: W3Schools At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. When you look at the example above, you can see that it will take a lot of code to produce a document with many headers and paragraphs. Normally XSL-FO document do not combine formatting information and content like we have done here. With a little help from XSLT we can put the formatting information into templates and write a cleaner content.
XSL-FO Lists XSL-FO uses List Blocks to define lists. XSL-FO List Blocks There are four XSL-FO objects used to create lists: • fo:list-block (contains the whole list) • fo:list-item (contains each item in the list)
7
• •
fo:list-item-label (contains the label for the list-item - typically an containing a number, character, etc.) fo:list-item-body (contains the content/body of the list-item - typically one or more objects)
An XSL-FO list example: * Volvo * Saab The output from this code would be: * Volvo * Saab
XSL-FO Tables XSL-FO uses the element to define tables. XSL-FO Tables The XSL-FO table model is not very different from the HTML table model. There are nine XSL-FO objects used to create tables: • fo:table-and-caption • fo:table • fo:table-caption • fo:table-column • fo:table-header • fo:table-footer • fo:table-body • fo:table-row fo:table-cell XSL-FO uses the element to define a table. It contains a and an optional element. The element contains optional elements, an optional element, a element, and an optional element. Each of these elements has one or more elements, with one or more elements:
8
Car Price Volvo $50000 SAAB $48000 The output from this code would something like this: Car Price Volvo $50000 SAAB $48000
XSL-FO and XSLT XSL-FO and XSLT can help each other. Remember this Example ? W3Schools At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. Result: W3Schools At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP.
The example above is from the chapter about XSL-FO Blocks.
9
With a Little Help from XSLT Remove the XSL-FO information from the document: <paragraph> At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP. Add an XSLT transformation: <xsl:template match="header"> <xsl:apply-templates/> <xsl:template match="paragraph"> <xsl:apply-templates/> And the result will be the same: W3Schools At W3Schools you will find all the Web-building tutorials you need, from basic HTML and XHTML to advanced XML, XSL, Multimedia and WAP.
XSL-FO Software XSL-FO needs formatting software to produce output. XSL-FO Processors An XSL-FO processor is a software program for formatting XSL documents for output. Most XSL-FO processors can output PDF documents and quality print, as well as HTML and other formats. Some well-known XSL-FO processors are described below. XSL Formatter XSL Formatter is a software to format XML documents for production-quality printing and output to PDF. Antenna House has been providing version V2 of the same product since January, 2002 in the global market, and XSL Formatter was rated as one of the best quality product at the XML 2002, XML 2003 conferences held in Europe. Building on over 4 years of experience developing XSL-FO software, Antenna House has completely written from scratch an entirely new Formatter that offers significant enhancements and provides a solid foundation on which to continue to move forward. Xinc Beta Release Xinc is an XSL-FO processor by Lunasil LTD. Xinc is designed to be fast, multithreaded and memory efficient. A Swing based XSL-FO viewer allows you to view and print XSL-FO files as well as generate PDF files with the click of a button. Xinc can be used as a server component via its Java API. Xinc can also be used in a Microsoft server environment by using its COM interface. New features include hyphenation, basic-link, PDF output, memory/speed optimizations and a simple COM interface.
10
Scriptura Inventive Designers Scriptura is a cross-platform document design and generation solution based on XSLT and XSL-FO. Scriptura has a WYSIWYG design tool and engine. The XSL-FO formatter used in the engine is no longer based on Apache FOP, but is written from scratch by Inventive Designers. The new features in this release are: support for bulleted and numbered lists, 'break-before' and 'break-after' properties, extended bar code options and improved number and currency formatting. A free trial version is available for download.
11
By: DataIntegratedEntity22592 Source: http://w3schools.com/xslfo/default.asp
12