我正在使用XSL FO将XML数据从文件转换为呈现为PDF文档的表格。如果 XML 中没有数据,我被要求呈现空表结构 - 将表正文留空。
我尝试了以下代码
<!-- Addresses -->
<fo:table border="1px solid black" table-layout="fixed" width="100%">
<fo:table-column column-width="2in"/>
<fo:table-column column-width="2in"/>
<fo:table-column column-width="2.5in"/>
<fo:table-column column-width="2.5in"/>
<fo:table-column column-width="2in"/>
<fo:table-header>
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="12pt" font-weight="bold" number-columns-spanned="5" background-color= "#B6B6B4"><fo:block>Addresses</fo:block></fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="10pt" font-weight="bold">
<fo:block> Address Type </fo:block>
</fo:table-cell>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="10pt" font-weight="bold">
<fo:block> Fields</fo:block>
</fo:table-cell>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="10pt" font-weight="bold">
<fo:block> Values </fo:block>
</fo:table-cell>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="10pt" font-weight="bold">
<fo:block> Data Source</fo:block>
</fo:table-cell>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="10pt" font-weight="bold">
<fo:block> DMC Doc Link</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:if test="core/clientMembers/address">
<xsl:apply-templates select="core/clientMembers/address"/>
</xsl:if>
<xsl:if test="not(core/clientMembers/address)">
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="9pt"><fo:block>Primary Address</fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="9pt"><fo:block>Incorporated Address</fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="9pt"><fo:block>Registered Address</fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="0.1pt solid black" text-align="left" font-size="9pt"><fo:block>Mailing Address</fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell border="0.1pt solid black"><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
</fo:table-row>
</xsl:if>
</fo:table-body>
</fo:table>
但是,这段代码没有按预期工作。 这里的专家能否建议是否有办法在 XSL FO ??? 中实现所需的行为 "核心/客户端主/覆盖"是 XSL 模板。
鉴于您呈现的代码,您所说的"将表体留空"是什么意思有点不清楚。 当然,我可以相信,如果 XPath 表达式core/clientMain/coverage
计算为空节点集,它不会做您想要的,因为这样它将在<fo:table-body>
元素内生成一个<fo:table-cell>
子元素,没有任何干预<fo:table-row>
。
我可以看到您想生成一行空单元格(显然其中五个以匹配五个标题),或者根本没有行,但是您呈现的内容没有意义。
"无行"的情况很简单:只需省略第二个xsl:if
元素及其内容,只留下
<xsl:if test="core/clientMain/coverage">
<xsl:apply-templates select="core/clientMain/coverage"/>
</xsl:if>
(我认为当实际上有数据时,该部分按预期工作。
如果实际上您想提供一个空的数据行,那么请提供它! 详细信息取决于您的特定需求,但可能如下所示:
<xsl:if test="core/clientMain/coverage">
<xsl:apply-templates select="core/clientMain/coverage"/>
</xsl:if>
<xsl:if test="not(core/clientMain/coverage)">
<fo:table-row>
<fo:table-cell><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
<fo:table-cell><fo:block white-space-treatment="preserve"><fo:leader /></fo:block></fo:table-cell>
</fo:table-row>
</xsl:if>