簡體   English   中英

XSL-FO中是否內置了“喜歡”的CSS?

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

我知道XSLT本身有屬性集,但這迫使我使用

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

每次我想輸出一個

<fo:something>

標簽。 XSL-FO規范中是否有任何內容允許我為FO輸出中的所有表指定(假設)一組默認屬性(邊距,填充等)?

基本上我正在尋找CSS的功能,但對於FO輸出而不是HTML。

不,您不需要使用xsl:element,如果將它放在XSLT命名空間中,則use-attribute-sets屬性可以出現在文字結果元素中,因此您可以使用以下內容:

<fo:something xsl:use-attribute-sets="myAttributeSet">

如果您想要接近CSS功能,那么您可以在處理結束時添加另一個XSLT轉換,以添加所需的屬性。 您可以從遞歸身份轉換開始,然后添加與要更改的元素匹配的模板,請參閱下面的小示例

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM