Transcript
Page 1: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Make Your Data Pretty with XSL-FO

Extensible Stylesheet Language-Formatting Objects

Page 2: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

What We Will Cover

• What is XSL-FO?• Different formatting engines• Introduction to XSL• Introduction to FO• Examples of using FO to create a PDF• Examples of using FO including XSL/XML to create a PDF• Example of using NFop in C# to create a PDF• Why our project switch from SSRS to XSL-FO

Page 3: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

What is XSL-FO?

XSL (eXtensible Stylesheet Language)A language written to provide formatting for XML documents.

FO (Formatting Objects)Containers of content that preserve structure and associate presentational information.

What do you use it for?XSL-FO is a markup language for XML document formatting which is most often used to generate PDFs.

Page 4: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

What is XSL-FO?

The XSL-FO language was designed for paged media.

FO works best for what could be called "content-driven" design. This is the standard method of layout for books, articles, legal documents, and so forth. It involves a single flowing span of fairly contiguous text, with various repeating information built into the margins of a page. This is as opposed to "layout-driven" design, which is used in newspapers or magazines. If content in those documents does not fit in the required space, some of it is trimmed away until it does fit. XSL-FO does not easily handle the tight restrictions of magazine layout; indeed, in many cases, it lacks the ability to express some forms of said layout.

Page 5: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

How XSL-FO Works

XML Document XSLT Document

XSLT Engine

FO Document

FO Engine

PDF Document

1. XML document and XSLT document are fed into an XSLT engine.2. Using the rules in the XSLT document, the XSLT engine tranforms the XML into a FO Document.3. The FO document is fed into the FO engine.4. The FO engine uses rules in the FO document to render the PDF.

Page 6: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

What can XSL-FO do?• Multiple columns• Lists - XSL-FO handles the numbering• Pagination controls - widows and orphans of text and

what is kept together on a page• Footnotes• Tables - much like HTML • Text orientation - different languages have different

text orientations• Page numbers - XSL-FO handles the numbering• Include images• Indexing - XSL-FO 1.1 supports creation of an index• Flows - XSL-FO 1.1 supports more than 1 flow

Page 7: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

AdvantagesXML language – Because it is an XML language, only an XSLT transform (and an XSLT processor) is required to generate XSL-FO code from any XML language.Ease of use – Another advantage of XSL-FO is the relative ease of use. Much of the functionality of the language is based on work from CSS.Low cost – Compared with commercial typesetting and page layout products, XSL-FO can offer a much lower cost solution. The initial cost of ownership is low (zero if the free implementations, such as Apache FOP, meet your requirements), especially compared to the cost of commercial composition tools. Multi-lingual – XSL-FO has been designed to work for all written human languages.Mature standard – With the publication of XSL-FO 1.1, XSL-FO is proving to be a mature standard with a number of solid commercial and non-commercial implementations.

Page 8: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

DrawbacksLimited capabilities – XSL-FO was specifically designed to meet the requirements of "lightly designed" documents typified by technical manuals, business documents, invoices, and so on. In particular, XSL-FO does not provide a direct way to get formatting effects that depend on knowing the page position relationship of two formatting objects. Missing features - there are important layout features that are simply not in XSL-FO.Extension dependency – one must consider proprietary extensions provided by the different XSL-FO implementations. These extensions add features that are not part of the core specification.

Page 9: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Different FOPs (Formatting Objects Processors)

Page 10: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Commercial Versions• Works with .Net• Good documentation and support• Can support things not in the FOP spec• Up to date with the spec

Examples• Ibex PDF Creator• AntennaHouse• RenderX• ASPOSE

Page 11: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Free VersionsApache.org Project's FOP - http://xmlgraphics.apache.org/fop/• Good documentation• Java app - need the open-source IKVM.NET project to run

Java apps on .NET. It is a Java virtual machine (JVM) on top of .NET.

• 'THE' standard in the open source processors• Always being updated

NFop - http://sourceforge.net/projects/nfop/• Ported to .Net via J#• Last update was in 2010• Easy to use (I'll show examples later)

Page 12: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Intro to XSL

Page 13: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Intro to XSLXSL (eXtensible Stylesheet Language)A language written to provide formatting for XML documents.

XSL consists of two parts:• A method for transforming XML documents• A method for formatting XML documents

XSLT (eXtensible Stylesheet Language for Transformations)This language takes existing XML documents and transforms them into other XML documents. For example, change XML into HTML, plain text or another XML document.

Page 14: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT ExampleTake a simple XML document: <?xml version="1.0"?> <name>Brian</name>

An XSLT document: <?xml version="1.0"?> <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0"> <body> <p>Hello <xsl:value-of select="name" /></p> </body> </html>

Page 15: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT Example<xsl:value-of select="name" /> indicates to replace this tag with the text at the node "name". "name" is referred to as the XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

Output:<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0"> <body> <p>Hello Brian</p> </body> </html>

Page 16: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT Example 2<?xml version="1.0"?><root> <person>

|<fname>Brian</fname>|

|<lname>Landers</lname>| </person> <person>

|<fname>Michelle</fname>|

|<lname>Landers</lname>| </person></root>

Page 17: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT Example 2<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" /><xsl:template match="/">

|<html><body>|

| | |<xsl:for-each select="root/person">|

| <p>Hello <xsl:value-of select="fname"/></p>|

|</xsl:for-each>|

|</body></html>|

Page 18: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT Example 2The <xsl:template> element contains rules to apply when a specified node is matched. match="/" defines the whole document.<xsl:for-each select="root/person"> indicates a loop and to do what is between the <xsl:for-each> tags for each instance of what is selected.

output:<html><body>

| | |<p>Hello Brian</p>|

| | |<p>Hello Michelle</p>| </body></html>

Page 19: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT if Test Example<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN" /><xsl:template match="/">

|<html><body>|

| | |<xsl:for-each select="root/person">|

|<xsl:if test="root/person/fname = 'Brian' ">|

| | |<p>Hello <xsl:value-of select="fname"/></p>|

|</xsl:if>|

|</xsl:for-each>|

Page 20: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

XSLT if Test Example<xsl:if test="root/person/name = 'Brian' "> is an if statement. if the condition is true you can continue, otherwise you skip to the </xsl:if>

output:<html><body>

| | |<p>Hello Brian</p>| </body></html>

Page 21: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Some Other XSL FunctionsFunctions on numeric values: abs, ceiling, floor, round, format-number

|<xsl:value-of select="round(Total)" />|

|<Total>123456.7</Total>|

|<xsl:value-of select="format-number(Total, '###,##0.00')" />|

|123,456.70|

functions on strings: compare, concat, substring, upper-case, lower-case, replace

|<xsl:value-of select="concat(fname,lname)" />|

Page 22: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Intro to FO

Page 23: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Document LayoutThe page is divided into regions.

region-body

region-before

region-after

region-

start

region-

end

Page 24: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Documents PartsAn XSL-FO document is divided into two main parts:

• One layout master set (layout-master-set), which specifies one or more page master templates (simple-page-master) to be used in the document.

• One or more page sequences, which contain the content of the document.

Page 25: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Documents Parts<?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="hello"> <!--name of page is "hello"--> <!-- Page template goes here - define your regions--> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="hello"> <!-- matches name of page from above--> <!-- Page content goes here --> </fo:page-sequence> </fo:root>

Page 26: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Hello World Example<?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="hello" page-height="8.5in" page-width="11in" margin-top="1in" margin-bottom="1in" margin-left="1in" margin-right="1in"> <fo:region-body margin-top="1in" margin-bottom=".5in"/> <!-- Page template goes here --> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="hello"> <fo:flow flow-name="xsl-region-body"> <!-- Page content goes here --> <fo:block font-size="18pt" text-align="center" font-weight="bold"> Hello World! </fo:block> </fo:flow> </fo:page-sequence> </fo:root>

Page 27: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Page-SequenceThe layout-master-set FO is followed by one or more page-sequence FOs, which must reference one of the simple-page-master templates by name. The page-sequence FOs contain the contents of the page or pages.

<fo:page-sequence master-reference="hello"> <!--Page contents--></fo:page-sequence>

Notice that the value of the master-reference attribute matches the master-name defined in the simple-page-master FO.

Page 28: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Flow and Static-ContentThe contents of the page inside the page-sequence FO are either static, meaning they appear the same on each page or flowing, meaning they flow from one page to the next. Static content is contained in static-content FOs and flowing content is contained in flow FOs.

<fo:page-sequence master-reference="hello"> <fo:flow flow-name="xsl-region-body"> <!--flowing content--> </fo:flow></fo:page-sequence>

This example only has a flow FO. In most cases, the region-body has flowing content and the other regions have static content.

Page 29: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

<fo:block>The block FO is used for separating and formatting generic blocks, such as paragraphs, tables or lists. Blocks can be nested.

Page 30: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Why Our Projects Switched From SSRS to XSL-FO

Page 31: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

SSRS to XSL-FOSSRS wasn't able to do what we wanted easily and as precisely.SSRS made some decisions on placement on it's own. The client has premade paper forms and needed placement to be exact.We needed one flow to go top-to-bottom and another flow to go bottom-to-top. SSRS couldn't do that.

Here's what we also got out of switching:• Needed to use custom DLL we created to help with reports.

This dll isn't needed in the new XSL-FO reports.• 3 RDL files that were very similar (about 90% the same)

were condensed to 1 XSL file that used data from XML file to determine which sections changed. Now 1 file only needs to be updated and all 3 reports stay in sync.

Page 32: Make Your Data Pretty With XSL-FO (Extensible Stylesheet Language)

Recommended