Welcome to our comprehensive tutorial on XSL-FO (Extensible Stylesheet Language for Formatting Objects)! In this lesson, we'll dive deep into the structure of XSL-FO documents, a powerful tool for creating formatted documents from XML data.
XSL-FO is an XML-based language used to define the formatting and layout of a document. It's designed to work with XSLT (XSL Transformations), another XML language used for transforming XML documents.
XSL-FO offers several benefits:
An XSL-FO document consists of several elements, which can be broadly categorized into four main sections:
fo:root. It contains all other elements in the document.<fo:root xmlns="http://www.w3.org/1999/XSL/Format">
<!-- Your XSL-FO content goes here -->
</fo:root>fo:layout-master-set and fo:page-sequence elements define the layout and background of the document.<fo:layout-master-set>
<!-- Define layout masters here -->
</fo:layout-master-set>
<fo:page-sequence master-reference="your-layout-master">
<!-- Content that should be repeated on each page goes here -->
</fo:page-sequence>fo:block (for block-level content), fo:inline (for inline content), and fo:table (for tabular data).<fo:block>This is a block of text.</fo:block>
<fo:inline>This is inline text.</fo:inline>
<fo:table>
<fo:table-body>
<!-- Table data goes here -->
</fo:table-body>
</fo:table>fo:font, fo:color, and fo:margin.<fo:font fo:font-size="12pt" fo:font-family="Arial" />
<fo:block font-family="Arial" color="#0000FF">This text is blue and in Arial.</fo:block>Let's create a simple XSL-FO document that formats a list of books:
<fo:root xmlns="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="book-page" page-height="8.5in" page-width="11in" margin="0.75in">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="book-page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="//book" />
</fo:flow>
</fo:page-sequence>
<xsl:template match="book">
<fo:block font-family="Arial" margin-bottom="1em">
<xsl:value-of select="title" />
<xsl:value-of select="author" />
</fo:block>
</xsl:template>
</fo:root>In this example, we define a simple page master, create a page sequence, and use an XSLT template to transform our XML data (represented by //book) into XSL-FO content.
What is the root element of an XSL-FO document?
That's it for this section! In the next lesson, we'll dive deeper into XSL-FO, exploring more formatting elements and advanced techniques. Stay tuned! 💡