我知道,XSLT本身具有的屬性集,但是這迫使我想每次使用在XSL-FO中是否有「像」CSS?
<xsl:element name="fo:something">
到輸出
<fo:something>
標籤。 XSL-FO規範中是否有任何內容允許我爲FO輸出中的所有表指定(比方說)默認的一組屬性(邊距,填充等)?
本質上我正在尋找CSS的功能,但對於FO輸出而不是HTML。
我知道,XSLT本身具有的屬性集,但是這迫使我想每次使用在XSL-FO中是否有「像」CSS?
<xsl:element name="fo:something">
到輸出
<fo:something>
標籤。 XSL-FO規範中是否有任何內容允許我爲FO輸出中的所有表指定(比方說)默認的一組屬性(邊距,填充等)?
本質上我正在尋找CSS的功能,但對於FO輸出而不是HTML。
不,你不需要使用xsl:元素,使用屬性集屬性可以在文字結果元素出現,如果你把它在XSLT命名空間,所以你可以使用類似:
<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>
不是在這個答案的底部遞歸身份的東西,有興趣的新事物代替舊的XSL-FO,但xsl:use-attribute-sets的簡單添加效果非常好。實際上,您可以像在(use-attribute-sets =「attributeSet1 attributeSet2」)中一樣添加多個屬性集引用,所以它實際上非常類似於CSS!優秀! – 2009-06-11 16:34:50
一個小例子,在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>
見https://stackoverflow.com/a/21345708/287948有關的Web標準的生態系統 – 2017-09-12 16:36:19