简体   繁体   中英

Using external CSS in 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, ie:

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.

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 (eg 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 (eg 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.

I'm not sure how you could remove them from the document entirely, but you could use xsl:attribute-set to move them out of the fo:table .

You could probably put them in a separate xsl file and then use a combination of xsl:include / xsl:import and xsl:call-template (your xsl:attribute-set could be put in a named template).

  <xsl:attribute-set name="table">
    <xsl:attribute name="border-bottom">solid 2pt #409C94</xsl:attribute>
    <xsl:attribute name="border-top">solid 2pt #409C94</xsl:attribute>
    <xsl:attribute name="margin-bottom">.1in</xsl:attribute>
    <!-- etc... -->
  </xsl:attribute-set>

to use them, add the attribute xsl:use-attribute-sets="table" to fo:table .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM