在XSL-FO中使用外部CSS

时间:2013-07-05 18:46:48

标签: css xml xslt xsl-fo

我正在使用XSL文档来创建PDF。有些样式定义为内联。我想在外部CSS文件中移动它们,但我正在走向死胡同。

这是我的代码:

<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>

我想要的是从本文档中删除所有样式标记,即:

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"

我正在考虑将它们移到CSS文件中,但欢迎任何更好的方法。

感谢。

2 个答案:

答案 0 :(得分:9)

根据Danial Haley提供的宝贵建议,我做了一些研究并找到了解决方案。它在下面供任何人参考。

带样式的文件(例如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>

我导入样式的主文件(例如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>

您可以在Main.xsl中看到,我已导入(也可能使用xsl:include)“样式表”Styles.xsl。在标记fo:table中,我添加了xsl:use-attribute-sets,它在VS2010中为Styles.xsl中定义的所有xsl:attribute-set提供了智能感知。

答案 1 :(得分:5)

我不确定如何完全从文档中删除它们,但您可以使用xsl:attribute-set将它们移出fo:table

您可以将它们放在单独的xsl文件中,然后使用xsl:include / xsl:importxsl:call-template的组合(您的xsl:attribute-set可以放在命名模板中)。

  <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>

要使用它们,请将属性xsl:use-attribute-sets="table"添加到fo:table