Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using external CSS in XSL-FO

Tags:

css

xml

xslt

xsl-fo

I am using an XSL document to create a PDF. There are some styles defined as inline. I want to move them in an external CSS file, but I am hitting a dead end.

Here is my code:

<fo:table border-bottom="solid 2pt #409C94" border-top="solid 2pt #409C94" margin-bottom=".1in" background-color="#E9E9E9" text-align="center"  table-layout="fixed" width="100%" font-size="9pt">
    <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>

What I want is to remove from this document are all the styling tags, i.e.:

border-bottom="solid 2pt #409C94"
border-top="solid 2pt #409C94"
margin-bottom=".1in"
background-color="#E9E9E9"
text-align="center"
table-layout="fixed"
width="100%" font-size="9pt"

I am thinking to move them in a CSS file but any better method will be welcomed.

Thanks.

like image 386
Farhan Avatar asked Jul 05 '13 18:07

Farhan


1 Answers

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.

like image 96
Farhan Avatar answered Sep 26 '22 00:09

Farhan