Is there something “like” CSS built into XSL-FO?

后端 未结 2 789
盖世英雄少女心
盖世英雄少女心 2021-02-08 14:30

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


every time I want to output an<

相关标签:
2条回答
  • 2021-02-08 15:21

    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>
    
    0 讨论(0)
  • 2021-02-08 15:23

    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>
    
    0 讨论(0)
提交回复
热议问题