XSL:FO 为什么 inline 不是 "fo:table" 的有效子项?我怎样才能解决这个问题?
XSL:FO why is inline is not a valid child of "fo:table"? How can I fix this?
问题:如何解决此错误 "XSL:FO inline is not a valid child of "fo:table"?
假设我有一个带有简单 table 的文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<diffreport>
<css />
<diff>
<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
<caption>This is the table caption</caption>
<tbody>
<tr>
<td>
<span class="diff-html-removed" id="removed-diff-0" previous="first-diff"
changeId="removed-diff-0" next="added-diff-0">one</span>
<span class="diff-html-added" id="added-diff-0" previous="removed-diff-0"
changeId="added-diff-0" next="removed-diff-1">uno</span>
</td>
<td>
<span class="diff-html-removed" id="removed-diff-1" previous="added-diff-0"
changeId="removed-diff-1" next="added-diff-1">two</span>
<span class="diff-html-added" id="added-diff-1" previous="removed-diff-1"
changeId="added-diff-1" next="last-diff">dos</span>
</td>
</tr>
<tr>
<td>three</td>
<td>four</td>
</tr>
</tbody>
</table>
<p />
</diff>
</diffreport>
我创建了一个 xsl:fo 模板文件,如下所示。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="default-page"
page-height="11in" page-width="8.5in" margin-left="0.6in"
margin-right="0.6in" margin-top="0.79in" margin-bottom="0.79in">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="default-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:block font-size="180%" font-weight="bold" text-align="center">
<xsl:text>Report</xsl:text>
</fo:block>
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- xsl report colors -->
<xsl:template match="span[@class='diff-html-added']">
<fo:inline color="green" background-color="#ccffcc">
<xsl:value-of select="." />
</fo:inline>
</xsl:template>
<xsl:template match="span[@class='diff-html-removed']">
<fo:inline color="red" text-decoration="line-through"
background-color="#fdc6c6">
<xsl:value-of select="." />
</fo:inline>
</xsl:template>
<!-- Table -->
<xsl:template match="table">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tbody">
<fo:table space-before="0.5em" space-after="0.5em"
table-layout="fixed">
<xsl:for-each select="tr[1]/th|tr[1]/td">
<xsl:apply-templates />
</xsl:for-each>
<fo:table-body>
<xsl:apply-templates />
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="tr">
<fo:table-row>
<xsl:if test="@space-before != ''">
<xsl:attribute name="space-before"><xsl:value-of select="@space-before" /></xsl:attribute>
</xsl:if>
<xsl:if test="@class='graybar'">
<xsl:attribute name="background-color">#ddd</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</fo:table-row>
</xsl:template>
<xsl:template match="th">
<fo:table-cell font-weight="bold" text-align="center">
<xsl:if test="ancestor::table/@border > 0">
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
</xsl:if>
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="td">
<fo:table-cell>
<xsl:if test="ancestor::table/@border > 0">
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
</xsl:if>
<fo:block>
<xsl:call-template name="set-alignment" />
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template name="set-alignment">
<xsl:choose>
<xsl:when test="@align='left' or contains(@class,'left')">
<xsl:attribute name="text-align">start</xsl:attribute>
</xsl:when>
<xsl:when test="@align='center' or contains(@class,'center')">
<xsl:attribute name="text-align">center</xsl:attribute>
</xsl:when>
<xsl:when test="@align='right' or contains(@class,'right')">
<xsl:attribute name="text-align">end</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我一直收到这个错误
(错误位置未知)org.apache.fop.fo.ValidationException:“{http://www.w3.org/1999/XSL/Format}inline”不是 "fo:table" 的有效子项! (没有可用的上下文信息)
如果我从 xsl 中删除这些代码块,错误就会消失,但我真的需要帮助来弄清楚为什么这段代码不起作用。
<xsl:template match="span[@class='diff-html-added']">
<span style="color:green; background-color: #ccffcc;">
<xsl:value-of select="."/></span>
</xsl:template>
为什么 inline 不是 "fo:table" 的子代?
我需要做什么来修复我的 xsl 模板,以便我的 span 类 可以在 table?
中工作
在 fo:table 之后生成 fo:table-header 和 fo:table-body 将解决您的问题:
<!-- Table -->
<xsl:template match="table">
<fo:table-and-caption>
<fo:table-caption>
<xsl:apply-templates select="caption"/>
</fo:table-caption>
<xsl:apply-templates select="tbody"/>
</fo:table-and-caption>
</xsl:template>
<xsl:template match="caption">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="tbody">
<fo:table space-before="0.5em" space-after="0.5em" table-layout="fixed">
<fo:table-header>
<xsl:apply-templates select="tr[1]"/>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="tr[position() > 1]"/>
</fo:table-body>
</fo:table>
</xsl:template>
结果:
问题:如何解决此错误 "XSL:FO inline is not a valid child of "fo:table"?
假设我有一个带有简单 table 的文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<diffreport>
<css />
<diff>
<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
<caption>This is the table caption</caption>
<tbody>
<tr>
<td>
<span class="diff-html-removed" id="removed-diff-0" previous="first-diff"
changeId="removed-diff-0" next="added-diff-0">one</span>
<span class="diff-html-added" id="added-diff-0" previous="removed-diff-0"
changeId="added-diff-0" next="removed-diff-1">uno</span>
</td>
<td>
<span class="diff-html-removed" id="removed-diff-1" previous="added-diff-0"
changeId="removed-diff-1" next="added-diff-1">two</span>
<span class="diff-html-added" id="added-diff-1" previous="removed-diff-1"
changeId="added-diff-1" next="last-diff">dos</span>
</td>
</tr>
<tr>
<td>three</td>
<td>four</td>
</tr>
</tbody>
</table>
<p />
</diff>
</diffreport>
我创建了一个 xsl:fo 模板文件,如下所示。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="default-page"
page-height="11in" page-width="8.5in" margin-left="0.6in"
margin-right="0.6in" margin-top="0.79in" margin-bottom="0.79in">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="default-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:block font-size="180%" font-weight="bold" text-align="center">
<xsl:text>Report</xsl:text>
</fo:block>
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- xsl report colors -->
<xsl:template match="span[@class='diff-html-added']">
<fo:inline color="green" background-color="#ccffcc">
<xsl:value-of select="." />
</fo:inline>
</xsl:template>
<xsl:template match="span[@class='diff-html-removed']">
<fo:inline color="red" text-decoration="line-through"
background-color="#fdc6c6">
<xsl:value-of select="." />
</fo:inline>
</xsl:template>
<!-- Table -->
<xsl:template match="table">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tbody">
<fo:table space-before="0.5em" space-after="0.5em"
table-layout="fixed">
<xsl:for-each select="tr[1]/th|tr[1]/td">
<xsl:apply-templates />
</xsl:for-each>
<fo:table-body>
<xsl:apply-templates />
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="tr">
<fo:table-row>
<xsl:if test="@space-before != ''">
<xsl:attribute name="space-before"><xsl:value-of select="@space-before" /></xsl:attribute>
</xsl:if>
<xsl:if test="@class='graybar'">
<xsl:attribute name="background-color">#ddd</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</fo:table-row>
</xsl:template>
<xsl:template match="th">
<fo:table-cell font-weight="bold" text-align="center">
<xsl:if test="ancestor::table/@border > 0">
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
</xsl:if>
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="td">
<fo:table-cell>
<xsl:if test="ancestor::table/@border > 0">
<xsl:attribute name="border-style">solid</xsl:attribute>
<xsl:attribute name="border-width">1pt</xsl:attribute>
</xsl:if>
<fo:block>
<xsl:call-template name="set-alignment" />
<xsl:apply-templates />
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template name="set-alignment">
<xsl:choose>
<xsl:when test="@align='left' or contains(@class,'left')">
<xsl:attribute name="text-align">start</xsl:attribute>
</xsl:when>
<xsl:when test="@align='center' or contains(@class,'center')">
<xsl:attribute name="text-align">center</xsl:attribute>
</xsl:when>
<xsl:when test="@align='right' or contains(@class,'right')">
<xsl:attribute name="text-align">end</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我一直收到这个错误 (错误位置未知)org.apache.fop.fo.ValidationException:“{http://www.w3.org/1999/XSL/Format}inline”不是 "fo:table" 的有效子项! (没有可用的上下文信息)
如果我从 xsl 中删除这些代码块,错误就会消失,但我真的需要帮助来弄清楚为什么这段代码不起作用。
<xsl:template match="span[@class='diff-html-added']">
<span style="color:green; background-color: #ccffcc;">
<xsl:value-of select="."/></span>
</xsl:template>
为什么 inline 不是 "fo:table" 的子代? 我需要做什么来修复我的 xsl 模板,以便我的 span 类 可以在 table?
中工作在 fo:table 之后生成 fo:table-header 和 fo:table-body 将解决您的问题:
<!-- Table -->
<xsl:template match="table">
<fo:table-and-caption>
<fo:table-caption>
<xsl:apply-templates select="caption"/>
</fo:table-caption>
<xsl:apply-templates select="tbody"/>
</fo:table-and-caption>
</xsl:template>
<xsl:template match="caption">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="tbody">
<fo:table space-before="0.5em" space-after="0.5em" table-layout="fixed">
<fo:table-header>
<xsl:apply-templates select="tr[1]"/>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="tr[position() > 1]"/>
</fo:table-body>
</fo:table>
</xsl:template>
结果: