Jay Stevens
Jay Stevens

Reputation: 5913

Is there something "like" CSS built into XSL-FO?

I know that XSLT itself has attribute-sets, but that forces me to use

<xsl:element name="fo:something">

every time I want to output an

<fo:something>

tag. Is there anything in the XSL-FO spec that would allow me to specify (let's say) a default set of attributes (margin, padding, etc.) for all Tables in the FO output?

Essentially I'm looking for the functionality of CSS, but for FO output instead of HTML.

Upvotes: 10

Views: 5168

Answers (2)

Joep
Joep

Reputation: 4133

In XSLT 2.0 there is also another option. The following template can be in a seperate file. You only need to include this file in the original xsl file that generates the FO structure.

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

    <xsl:template match="/" priority="1000">
        <!-- Store generated xsl-fo document in variable-->
        <xsl:variable name="xsl-fo-document">
            <xsl:next-match/>
        </xsl:variable>

        <!-- Copy everything to result document and apply "css" -->
        <xsl:apply-templates select="$xsl-fo-document" mode="css"/>
    </xsl:template>

    <xsl:template match="@*|node()" priority="1000" mode="css">
        <xsl:param name="copy" select="true()" tunnel="yes"/>
        <xsl:if test="$copy">
            <xsl:copy>
                <xsl:next-match>
                    <xsl:with-param name="copy" select="false()" tunnel="yes"/>
                </xsl:next-match>
                <xsl:apply-templates select="@*|node()" mode="css"/>
            </xsl:copy>
            </xsl:if>
    </xsl:template>

    <!-- **************************** -->
    <!-- CSS Examples (e.g. fo:table) -->
    <!-- **************************** -->

    <xsl:template match="fo:table-cell[not(@padding)]" mode="css">
        <xsl:attribute name="padding" select="'2pt'"/>
        <xsl:next-match/>
    </xsl:template>

    <xsl:template match="fo:table-header/fo:table-row/fo:table-cell" mode="css">
        <xsl:attribute name="color" select="'black'"/>
        <xsl:attribute name="font-style" select="'bold'"/>
        <xsl:next-match/>
    </xsl:template>

</xsl:transform>

Upvotes: 0

George Bina
George Bina

Reputation: 1161

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>

Upvotes: 11

Related Questions