XSL-FO 中是否有类似于 CSS 的东西?

10

我知道XSLT本身具有属性集,但那会强制我使用

<xsl:element name="fo:something">
每次我想要输出一个
<fo:something>

在XSL-FO规范中是否有任何内容可以允许我为所有表格指定(例如)默认的属性集(边距,填充等)?

本质上,我正在寻找类似于CSS的功能,但用于FO输出而不是HTML。


请参考 https://dev59.com/-mgv5IYBdhLWcg3wYvyi#21345708,了解如何用Web标准生态系统中的新技术替换旧的XSL-FO。 - Peter Krauss
2个回答

11
不需要使用 xsl:element,可以在文字结果元素上使用 use-attribute-sets 属性,只需将其置于 XSLT 命名空间中即可。因此,您可以使用类似以下的内容:
<fo:something xsl:use-attribute-sets="myAttributeSet">
如果您想要类似于CSS的功能,那么可以在处理结束时添加另一个XSLT转换,以添加所需的属性。您可以从递归的identity转换开始,然后添加匹配您想更改的元素的模板,下面是一个简单的示例。
<?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>

不太关心答案底部的递归身份验证内容,但是简单添加xsl:use-attribute-sets非常有效。实际上,您可以添加多个属性集引用,例如(use-attribute-sets="attributeSet1 attributeSet2"),因此它非常类似于CSS!太棒了! - Jay Stevens

0
在XSLT 2.0中还有另一个选项。以下模板可以放在单独的文件中。您只需要将此文件包含在生成FO结构的原始xsl文件中即可。
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接