You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

informaltable单元格背景色不生效求助(XML/XSLT转PDF)

DocBook转PDF:informaltable单元格背景色不生效的解决方法

问题

处理DocBook XML生成PDF时,给标记了role="blue_table"的<informaltable>中第一列单元格设置背景色,但始终显示白色,代码语法无错误。

现有代码

XSLT片段

<?xml version="1.0" encoding="UTF-8"?>
<?stylesheet-label Adasa's template for PDF and PostScript creation?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                xmlns:d="http://docbook.org/ns/docbook"
                version="1.0">
<xsl:import href="docbook5-config:xsl/fo/docbook.xsl"/>
<xsl:output method="xml"/>

    <xsl:attribute-set name="table.table.properties">
      <xsl:attribute name="border-before-width.conditionality">retain</xsl:attribute>
      <xsl:attribute name="border-collapse">collapse</xsl:attribute>
    </xsl:attribute-set>
    
    <xsl:template match="informaltable[@role='blue_table']">
     <xsl:apply-templates mode="blue_table"/>
    </xsl:template>
    
    <xsl:template match="row/entry[1]/para" mode="blue_table">
      <xsl:attribute name="background-color">#0E2F48</xsl:attribute>
      <xsl:attribute name="color">#FF0000</xsl:attribute>
    </xsl:template>

XML片段

<?xml version="1.0" encoding="UTF-8"?>
<section version="5.1" xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         xmlns:trans="http://docbook.org/ns/transclusion"
         xmlns:svg="http://www.w3.org/2000/svg"
         xmlns:m="http://www.w3.org/1998/Math/MathML"
         xmlns:html="http://www.w3.org/1999/xhtml"
         xmlns:db="http://docbook.org/ns/docbook">
  <title>工作团队</title>
       
  <para>以下描述媒体说明中出现的每个职位的职能和职责。</para>

  <informaltable role="blue_table">
    <tgroup cols="2">
      <colspec colname="c1" colwidth="310*"/>
      <colspec colname="c2" colwidth="690*"/>

      <tbody>
        <row>
          <entry valign="middle">
            <para>承包商代表</para>
          </entry>

问题根源

你当前的模板是给<para>元素添加背景色,但在XSL-FO规范中,单元格的背景色需要设置在fo:table-cell元素上——<para>对应的FO元素是fo:block,设置它的背景只会覆盖段落文本区域,无法填充整个单元格,甚至会被单元格的默认样式覆盖。

修正方案

直接匹配<entry>节点,调用DocBook自带的entry模板并传递样式参数,让生成的fo:table-cell带上背景色:

<!-- 替换原有的para匹配模板 -->
<xsl:template match="row/entry[1]" mode="blue_table">
  <xsl:call-template name="entry">
    <xsl:with-param name="properties">
      <xsl:attribute name="background-color">#0E2F48</xsl:attribute>
      <xsl:attribute name="color">#FF0000</xsl:attribute>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

如果需要复用样式,可以定义属性集:

<xsl:attribute-set name="blue-table-cell-properties">
  <xsl:attribute name="background-color">#0E2F48</xsl:attribute>
  <xsl:attribute name="color">#FF0000</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="row/entry[1]" mode="blue_table">
  <xsl:call-template name="entry">
    <xsl:with-param name="properties" select="document('')/*/xsl:attribute-set[@name='blue-table-cell-properties']/*"/>
  </xsl:call-template>
</xsl:template>

额外检查点

  • 确认border-collapse属性已设置为collapse,避免边框遮挡背景色;
  • 检查FO处理器(如Apache FOP)版本,旧版本可能存在背景色渲染bug;
  • 如果单元格内有多个<para>,上述方案会确保整个单元格背景统一生效。

内容的提问来源于stack exchange,提问作者Albert López

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:01:25