我有一个.xsl
这样的文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:exslt="http://exslt.org/common">
<xsl:template match="/>
<fo:root>
<fo:block>...</fo:block>
</fo:root>
</xsl:template>
</xsl:stylesheet>
如何使用模板来匹配和设置生成的fo
元素的样式?例如,如果我想给我fo:table-cell
的红色背景,我希望能够做到
<xsl:template match="fo:table-cell">
<xsl:attribute name="background-color">red</xsl:attribute>
</xsl:template>
我找到了这个,然后尝试了一些类似的东西
<xsl:template match="/>
<xsl:variable name="foRoot">
<fo:root>
<fo:block>...</fo:block>
</fo:root>
</xsl:variable>
<xsl:apply-templates select="exslt:node-set($foRoot)" />
</xsl:template>
但这会由于无限递归而导致堆栈溢出。当我试图避免这种情况时,例如通过做
<xsl:apply-templates select="exslt:node-set($foRoot)/*" />
我得到一个空文件。当试图通过添加来解决这个问题时
<xsl:copy-of select="$foRoot" />
紧接着,我没有收到任何错误,但表格单元格仍然具有默认的白色背景。
如果您真的使用 XSLT 2 处理器,那么首先您不需要exsl:node-set
.
至于你的模板
<xsl:template match="fo:table-cell">
<xsl:attribute name="background-color">red</xsl:attribute>
</xsl:template>
这将匹配 FOtable-cell
但将其转换为属性。所以你宁愿
<xsl:template match="fo:table-cell">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="background-color">red</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
因为这会将属性添加到元素的浅表副本中,然后使用apply-templates
.
当然,您还需要添加身份转换模板
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
以确保您不想更改的元素被复制。如果您拥有的其他模板干扰了身份转换,则可能需要使用模式来分隔处理步骤。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句