Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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


Published on

Do you have XML data that you need formatted in HTML or PDF format (or both)?
Do you have a document where alternating pages (left/right or front/back) have a slightly different format?
What about a document that has a fixed header/footer on each page and the middle is where your text goes?

Do you want a little more flexibility in formatting PDFs than SSRS gives you?
For this lunch and learn we will explore the following:
XSL basics
What are Formatting Objects in XSL-FO
Using XSL-FO in .Net to transform XML data into a PDF

Published in: Software, Education
  • Be the first to comment

  • Be the first to like this

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

  1. 1. Make Your Data Pretty with XSL-FO Extensible Stylesheet LanguageFormatting Objects
  2. 2. 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
  3. 3. 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.
  4. 4. 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.
  5. 5. 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.
  6. 6. 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
  7. 7. Advantages XML 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 noncommercial implementations.
  8. 8. Drawbacks Limited 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, XSLFO 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.
  9. 9. Different FOPs (Formatting Objects Processors)
  10. 10. 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
  11. 11. Free Versions Project's 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 - • Ported to .Net via J# • Last update was in 2010 • Easy to use (I'll show examples later)
  12. 12. Intro to XSL
  13. 13. Intro to XSL XSL (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.
  14. 14. XSLT Example Take a simple XML document: <?xml version="1.0"?> <name>Brian</name> An XSLT document: <?xml version="1.0"?> <html xmlns:xsl="" xsl:version="1.0"> <body> <p>Hello <xsl:value-of select="name" /></p> </body> </html>
  15. 15. 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="" xsl:version="1.0"> <body> <p>Hello Brian</p> </body> </html>
  16. 16. 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>
  17. 17. XSLT Example 2 <xsl:stylesheet version="1.0" xmlns:xsl=""> <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>|
  18. 18. XSLT Example 2 The <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>
  19. 19. XSLT if Test Example <xsl:stylesheet version="1.0" xmlns:xsl=""> <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>|
  20. 20. 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>
  21. 21. Some Other XSL Functions Functions 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)" />|
  22. 22. Intro to FO
  23. 23. Document Layout The page is divided into regions. region-before region start region-body region-after region end
  24. 24. Documents Parts An 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.
  25. 25. Documents Parts <?xml version="1.0" encoding="utf-8"?> <fo:root xmlns:fo=""> <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>
  26. 26. Hello World Example <?xml version="1.0" encoding="utf-8"?> <fo:root xmlns:fo=""> <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>
  27. 27. Page-Sequence The 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.
  28. 28. Flow and Static-Content The 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.
  29. 29. <fo:block> The block FO is used for separating and formatting generic blocks, such as paragraphs, tables or lists. Blocks can be nested.
  30. 30. Why Our Projects Switched From SSRS to XSL-FO
  31. 31. SSRS to XSL-FO SSRS 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 bottomto-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.