【问题】
docbook中生成的pdf中的table的第一行,一般是firstrow或thead(table header)关键字的,没有与其他内容区别开来,显示上无特殊效果。希望给设置背景色等。
【解决过程】
1.这里2. FO Parameter Reference面的参数,都只有table的边框的颜色。没有背景色的设置参数。
2.后来想到,参考之前做法,找到docbook-xsl-ns-1.76.1\fo中对应的xsl,然后提取出来相关的配置。
后来找到了是table.xsl,找到其中的相关设置,然后修改为:
<!-- from docbook-xsl-ns-1.76.1\fo\table.xsl --> <!-- Expand this template to add properties to any cell's block --> <xsl:template name="table.cell.block.properties"> <!-- highlight this entry? --> <xsl:choose> <xsl:when test="ancestor::d:thead or ancestor::d:tfoot"> <!-- <xsl:attribute name="font-weight">bold</xsl:attribute> --> <xsl:attribute name="background-color">silver</xsl:attribute> </xsl:when> <!-- Make row headers bold too --> <xsl:when test="ancestor::d:tbody and (ancestor::d:table[@rowheader = 'firstcol'] or ancestor::d:informaltable[@rowheader = 'firstcol']) and ancestor-or-self::d:entry[1][count(preceding-sibling::d:entry) = 0]"> <!-- <xsl:attribute name="font-weight">bold</xsl:attribute> --> <xsl:attribute name="background-color">silver</xsl:attribute> </xsl:when> </xsl:choose> </xsl:template>
然后放到自己的xsl文件docbook_crl.xsl中去,最后是可以生成给table的第一行添加背景色的效果的:
但是可以看出,有些行,是没有完全铺满对应的cell的。
所以,还是继续折腾希望可以找到办法,实现背景色是铺满对应的cell的。
2.后来参考:Modifying table characteristics,添加:
<xsl:attribute-set name="thead.row.entry"> <!--head cell--> <xsl:attribute name="background-color">antiquewhite</xsl:attribute> </xsl:attribute-set>
但是结果是生成的pdf中,没有效果。
3.后又参考:Re: [docbook-apps] Table ‘thead’ cell Color,然后把下面内容:
<xsl:template match="thead"> <xsl:variable name="tgroup" select="parent::*"/> <fo:table-header> <!-- ADD THESE TWO PROPERTIES --> <xsl:attribute name="background-color">red</xsl:attribute> <xsl:apply-templates select="row[1]"> <xsl:with-param name="spans"> <xsl:call-template name="blank.spans"> <xsl:with-param name="cols" select="../@cols"/> </xsl:call-template> </xsl:with-param> </xsl:apply-templates> </fo:table-header> </xsl:template>
添加到xsl中,结果也是没效果。
4.后来参考:Re: [docbook-apps] Table head output in FO,折腾了一下,写了下面代码:
<!-- copy from table.xsl --> <!-- Expand this template to add properties to any fo:table-cell --> <xsl:template name="table.cell.properties"> <xsl:param name="bgcolor.pi" select="''"/> <xsl:param name="rowsep.inherit" select="1"/> <xsl:param name="colsep.inherit" select="1"/> <xsl:param name="col" select="1"/> <xsl:param name="valign.inherit" select="''"/> <xsl:param name="align.inherit" select="''"/> <xsl:param name="char.inherit" select="''"/> <xsl:choose> <!-- new add begin --> <xsl:when test="ancestor::d:thead"> <xsl:attribute name="background-color">antiquewhite</xsl:attribute> </xsl:when> <!-- new add end --> <xsl:when test="ancestor::d:tgroup"> <xsl:if test="$bgcolor.pi != ''"> <xsl:attribute name="background-color"> <xsl:value-of select="$bgcolor.pi"/> </xsl:attribute> </xsl:if> <xsl:if test="$rowsep.inherit > 0"> <xsl:call-template name="border"> <xsl:with-param name="side" select="'bottom'"/> </xsl:call-template> </xsl:if> <xsl:if test="$colsep.inherit > 0 and $col < (ancestor::d:tgroup/@cols|ancestor::d:entrytbl/@cols)[last()]"> <xsl:call-template name="border"> <xsl:with-param name="side" select="'end'"/> </xsl:call-template> </xsl:if> <xsl:if test="$valign.inherit != ''"> <xsl:attribute name="display-align"> <xsl:choose> <xsl:when test="$valign.inherit='top'">before</xsl:when> <xsl:when test="$valign.inherit='middle'">center</xsl:when> <xsl:when test="$valign.inherit='bottom'">after</xsl:when> <xsl:otherwise> <xsl:message> <xsl:text>Unexpected valign value: </xsl:text> <xsl:value-of select="$valign.inherit"/> <xsl:text>, center used.</xsl:text> </xsl:message> <xsl:text>center</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> </xsl:if> <xsl:choose> <xsl:when test="$align.inherit = 'char' and $char.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$char.inherit"/> </xsl:attribute> </xsl:when> <xsl:when test="$align.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$align.inherit"/> </xsl:attribute> </xsl:when> </xsl:choose> </xsl:when> <xsl:otherwise> <!-- HTML table --> <xsl:if test="$bgcolor.pi != ''"> <xsl:attribute name="background-color"> <xsl:value-of select="$bgcolor.pi"/> </xsl:attribute> </xsl:if> <xsl:if test="$align.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$align.inherit"/> </xsl:attribute> </xsl:if> <xsl:if test="$valign.inherit != ''"> <xsl:attribute name="display-align"> <xsl:choose> <xsl:when test="$valign.inherit='top'">before</xsl:when> <xsl:when test="$valign.inherit='middle'">center</xsl:when> <xsl:when test="$valign.inherit='bottom'">after</xsl:when> <xsl:otherwise> <xsl:message> <xsl:text>Unexpected valign value: </xsl:text> <xsl:value-of select="$valign.inherit"/> <xsl:text>, center used.</xsl:text> </xsl:message> <xsl:text>center</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> </xsl:if> <xsl:call-template name="html.table.cell.rules"/> </xsl:otherwise> </xsl:choose> </xsl:template>
对应的效果,背景色的确是覆盖了cell,但是却又破坏了原先的设置了:
所以,还是不行。还要继续想办法。
最后经过一番折腾,改成如下代码:
<!-- copy from docbook-xsl-ns-1.76.1\fo\table.xsl --> <!-- Expand this template to add properties to any fo:table-cell --> <xsl:template name="table.cell.properties"> <xsl:param name="bgcolor.pi" select="''"/> <xsl:param name="rowsep.inherit" select="1"/> <xsl:param name="colsep.inherit" select="1"/> <xsl:param name="col" select="1"/> <xsl:param name="valign.inherit" select="''"/> <xsl:param name="align.inherit" select="''"/> <xsl:param name="char.inherit" select="''"/> <xsl:choose> <xsl:when test="ancestor::d:tgroup"> <!-- new add begin --> <xsl:if test="ancestor::d:thead"> <xsl:attribute name="background-color">antiquewhite</xsl:attribute> </xsl:if> <!-- new add end --> <xsl:if test="$bgcolor.pi != ''"> <xsl:attribute name="background-color"> <xsl:value-of select="$bgcolor.pi"/> </xsl:attribute> </xsl:if> <xsl:if test="$rowsep.inherit > 0"> <xsl:call-template name="border"> <xsl:with-param name="side" select="'bottom'"/> </xsl:call-template> </xsl:if> <xsl:if test="$colsep.inherit > 0 and $col < (ancestor::d:tgroup/@cols|ancestor::d:entrytbl/@cols)[last()]"> <xsl:call-template name="border"> <xsl:with-param name="side" select="'end'"/> </xsl:call-template> </xsl:if> <xsl:if test="$valign.inherit != ''"> <xsl:attribute name="display-align"> <xsl:choose> <xsl:when test="$valign.inherit='top'">before</xsl:when> <xsl:when test="$valign.inherit='middle'">center</xsl:when> <xsl:when test="$valign.inherit='bottom'">after</xsl:when> <xsl:otherwise> <xsl:message> <xsl:text>Unexpected valign value: </xsl:text> <xsl:value-of select="$valign.inherit"/> <xsl:text>, center used.</xsl:text> </xsl:message> <xsl:text>center</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> </xsl:if> <xsl:choose> <xsl:when test="$align.inherit = 'char' and $char.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$char.inherit"/> </xsl:attribute> </xsl:when> <xsl:when test="$align.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$align.inherit"/> </xsl:attribute> </xsl:when> </xsl:choose> </xsl:when> <xsl:otherwise> <!-- HTML table --> <xsl:if test="$bgcolor.pi != ''"> <xsl:attribute name="background-color"> <xsl:value-of select="$bgcolor.pi"/> </xsl:attribute> </xsl:if> <xsl:if test="$align.inherit != ''"> <xsl:attribute name="text-align"> <xsl:value-of select="$align.inherit"/> </xsl:attribute> </xsl:if> <xsl:if test="$valign.inherit != ''"> <xsl:attribute name="display-align"> <xsl:choose> <xsl:when test="$valign.inherit='top'">before</xsl:when> <xsl:when test="$valign.inherit='middle'">center</xsl:when> <xsl:when test="$valign.inherit='bottom'">after</xsl:when> <xsl:otherwise> <xsl:message> <xsl:text>Unexpected valign value: </xsl:text> <xsl:value-of select="$valign.inherit"/> <xsl:text>, center used.</xsl:text> </xsl:message> <xsl:text>center</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:attribute> </xsl:if> <xsl:call-template name="html.table.cell.rules"/> </xsl:otherwise> </xsl:choose> </xsl:template>
就终于可以实现我所想要的效果,即整个cell都充满了背景色了:
【总结】
想要实现给table的firstrow添加背景色,主要是利用到thead整个属性,然后把下面代码:
<xsl:if test="ancestor::d:thead"> <xsl:attribute name="background-color">antiquewhite</xsl:attribute> </xsl:if>
添加到table.xsl中合适的位置即可。
转载请注明:在路上 » 【已解决】给docbook中的表格(table)的第一行(firstrow或thead)添加背景色