XSL:FO为什么内联不是" fo:table&#34 ;?的有效子元素?我怎样才能解决这个问题?

时间:2016-08-10 00:59:50

标签: xml xslt xsl-fo

问题:如何解决此错误" XSL:FO inline不是" fo: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 &gt; 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 &gt; 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:&#34; {http://www.w3.org/1999/XSL/Format}内联&#34;不是&#34; fo:table&#34;的有效孩子! (没有上下文信息)

如果我从xsl中删除这些代码块,则错误消失,但我真的需要帮助来弄清楚为什么这段代码不起作用。

  <xsl:template match="span[@class='diff-html-added']">
     <span style="color:green; background-color: #ccffcc;">
     <xsl:value-of select="."/></span>
 </xsl:template>

为什么内联不是&#34; fo:table&#34;的孩子? 我需要做些什么来修复我的xsl模板,以便我的span类可以在表中工作?

1 个答案:

答案 0 :(得分:2)

在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() &gt; 1]"/>
        </fo:table-body>
    </fo:table>
</xsl:template>

结果:

enter image description here