Is There Something "Like" CSS Built into Xsl-Fo

Is there something like CSS built into XSL-FO?

No, you are not required to use xsl:element, the use-attribute-sets attribute can appear on literal result elements if you place it in the XSLT namespace, so you can use something like:

<fo:something xsl:use-attribute-sets="myAttributeSet">

If you want to have something close to the CSS functionality then you can add another XSLT transformation at the end of your processing that adds the attributes that you want. You can start with a recursive identity transformation and then add templates matching on the elements you want to change, see a small example below

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:attribute-set name="commonAttributes">
<xsl:attribute name="common">value</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="someElement">
<xsl:copy use-attribute-sets="commonAttributes">
<xsl:attribute name="someAttribute">someValue</xsl:attribute>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

How to style all the same XSL FO elements? XSL FO like CSS?

Normally you are creating XSL FO from XML and XSL and that would not apply because you would do something in an template-match for whatever creates your table-row. So you would only be writing it once.

Now, you could use the same concept and create an identity XSL that modifies your XSL FO input to a new XSL FO and uses <xsl:template match="fo:table-row"> and applies all your rules.

Use of XSL-FO, CSS3 instead of CSS2 to create Paginated documents like PDF?

Thanks all comments and answers!

Now, 2014, passed over 1.5 years of my post (May 17 '12), is time to consolidate: no answer was, for me, a "full answer", but all answers (see Nenotlep's and Alex's) contributed to form a big picture.
My main motivation now, to consolidate, is the @mzjn's news (here) of 2013-11.

XSL-FO is officially dying

On Sat, 2013-11-02, Liam R. E. Quin wrote:
"We have closed the Working Group because not enough people were taking part", W3C XML Activity Lead, about the failure of XSL-FO 2.0 continuity. (see a better copy here).

The last update for the Working Draft was in January 2012, and now confirmed: W3C stop developing XSL-2.

Why? It will be replaced by CSS3-page, see below.

PS: to discuss the "official statment", use https://stackoverflow.com/a/21345449/287948

CSS3 is officially growing

The standard CSS3-page is a draft, but many applications, like PrinceXML v9 and AntennaHouse Formatter v6 demonstrated that it is ready (!); and, the expected launch of HTML5 for 2014 is carrying along the forecast release CSS3.

So, I understand that for W3C, CSS3-page do all that we need to express good prints and good PDF.

Other motivations

One day, in a far future... PDF will dead — it is complex and is not part of the XML family or W3C investments —, and many claim that EPUB will replace it.
This is another good motivation: tablet readers and PC browsers will print (HTML, XHTML and EPUB) as well as PDF. So PDF will be not necessary... And, for this day, the only standard need for, ex. Webkit printing project, will be the CSS3-page standard.

CSS3 is the key point in two strategic affairs: 1) to generate good PDF from XML or HTML contents; 2) to replace PDF.


NOTE: another 2014's updates for the links of the question: wkHtmlToPDF is now here. About "new texts", now we have many, see ex. Building Books with CSS3.



An updated answer for programmers, for this page's question, Why use XSL-FO instead of CSS2, for transform HTML into good PDF?

If you go further and implement a new system for XML-Publishing, there are no good reason to use XSL-FO. SUMMARIZING:

  • XSL-FO is a dead technology today, only used by niche companies, to give maintenance to legacy systems in big publishing companies, like Elsevier... Most writers/readers of Stackoverflow are from small and medium companies. Companies like O'Reilly Media, Inc. already use CSS3 for print.

  • CSS3 will replace CSS2, covering all gaps (and fears as @AlexS's) of CSS2.

  • today (2014), as you can check by Google or my links (see PrinceXML v9 and AntennaHouse Formatter v6), we have some good software to render content with CSS2 or CSS3.

  • as @bytebuster say, "CSS is much easier to develop" (and easier to learn!).

  • as I say above, CSS3 is not isolated, it is a piece of the "XML/HTML/SVG" family.

  • is much cheaper to develop "HTML+CSS templates" (hourly cost of a standard web designer doing a simple task), than "XSL-FO templates" (hourly cost of a rare professional in a complex task).

  • ....



News...

Jan'2016, the definitive CSS3 standard is coming!

About W3C standards: the old "css-page" was replaced by "css-break", and "paged media" to "fragmentation"... Now it is a Candidate Recommendation, see https://www.w3.org/TR/css-break-3

Apr'2020, Blimey, +4 years and nothing!... Ok, need more tests

Total 8 years from question's post, and 4 years from "css-break-3 fineshed!" announcement ...

Chrome was the first to finesh in 2019 but some was wrong in test validation team of W3C, and in 2020 back... Now the status (in 23 tests) is:

  • Chrome's Blink engine fail 1 test;
  • Firefox's Gecko engine fail 3 tests.

xxx

The draft now is here and tests here.

Using external CSS in XSL-FO

With the valuable suggestion provided by Danial Haley, I did some research and found the solution. It is below for anyone's reference.

File with styles (e.g. Styles.xsl)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:attribute-set name="CustomStyles">
<xsl:attribute name="background-color">#BB5588</xsl:attribute>
<xsl:attribute name="border-bottom">solid 2pt #409C94</xsl:attribute>
<xsl:attribute name="border-top">solid 2pt #409C94</xsl:attribute>
<xsl:attribute name="font-size">9pt</xsl:attribute>
</xsl:attribute-set>

My main file where I am importing styles (e.g. Main.xsl)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="Styles.xsl"/>

<fo:table xsl:use-attribute-sets="CustomStyles" margin-bottom=".1in" text-align="center" table-layout="fixed" width="100%">
<fo:table-column column-width="proportional-column-width(100)"/>
<fo:table-body width="100%" table-layout="fixed">
<fo:table-row>
<fo:table-cell text-align="center" padding-top=".5mm" padding-bottom=".5mm">
<fo:block>Some text is placed here.</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>

Here you can see in Main.xsl, that I have a imported (could also have used xsl:include) the "stylesheet" Styles.xsl. In the tag fo:table, I added xsl:use-attribute-sets, which in VS2010, provided intellisense for all the xsl:attribute-set defined in Styles.xsl.

Can XSLT do everything that CSS can do?

XSLT is nothing like CSS.

CSS details the way that a given markup is rendered in a given media (on screen, on paper, to loudspeakers, etc).

XSLT turns one XML document into another document (XML, HTML or plain text, most often XML or HTML).

XSL more generally started with more CSS-like goals, of detailing how an XML document should be presented. This project grew into several different projects with XSL-FO fulfilling this rôle, and XSLT doing the transformations I mentioned above. It was originally designed to help with the XSL-FO stuff (XSLT would turn some XML into XSL-FO that would detail rendering), but now has many uses outside of that, so it made sense to split it off.

CSS can be used directly on XML, so the closest thing to CSS in the XML world, is CSS.

Using XSL-FO and HTML?

XSL-FO is for PDF display only (this is not strictly true, but you can take it as a guideline). So HTML output and FO output are not related. From your XML source, you can use XSLT to generate XHTML or XSL-FO, but not both at the same time.

See for example DocBook. It comes with several XSLT stylesheets ready to use, one is for HTML output and one for PDF (via Apache Fop). If you are satisfied with the result could be a different question.



Related Topics



Leave a reply



Submit