我有一个.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>
将匹配fo table-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>
确保您不想更改的元素已复制。如果您的其他模板干扰身份转换,则可能有必要使用模式分开处理步骤。