Java xsl-xmlfo文件-将表数据输入xml
我有XSLXML文件,我使用fop工厂javax.xml.transform.TransformerFactory将其转换为fop文件,因此最终我可以将其转换为PDF。到目前为止,我只需要输入简单的数据,因此在xsl中我会编写如下内容:Java xsl-xmlfo文件-将表数据输入xml,java,xml,xsl-fo,Java,Xml,Xsl Fo,我有XSLXML文件,我使用fop工厂javax.xml.transform.TransformerFactory将其转换为fop文件,因此最终我可以将其转换为PDF。到目前为止,我只需要输入简单的数据,因此在xsl中我会编写如下内容: <xsl:template match="p"> <fo:block> <xsl:apply-templates/> </fo:block> </xsl:template>
<xsl:template match="p">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="center">
<fo:block alignment-adjust="middle">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="b">
<fo:inline font-weight="bold">
<xsl:apply-templates/>
</fo:inline>
</xsl:template>
等等。
当我将其转换为fop文件时,我得到:
<fo:block text-align="center" font-weight="bold"
text-decoration="underline"
padding-after="10">Sales Contract for <fo:inline font-weight="bold"
color="red">Mobile 100</fo:inline>
</fo:block>
等等
现在有一种情况,我需要显示一个包含数据的表。如何在XML中定义表模板,以及在XML中添加什么?我不明白您想要什么…但在每种情况下,我都在编写一个表的示例。 假设您有一个格式如下的表,其中标记行表示表中的行,标记条目表示列:
<table ENABLE="true" id="" title="">
<tgroup ENABLE="true" cols="">
<tbody ENABLE="true">
<row ENABLE="true">
<entry ENABLE="true" align="0 left">
<text ENABLE="true">
<DATA ENC="enc" LABEL="label">First row, first column (entry)</DATA>
</text>
</entry>
<entry ENABLE="true" align="0 left">
<text ENABLE="true">
<DATA ENC="enc" LABEL="label">First row, second column (entry)</DATA>
</text>
</entry>
</row>
<row ENABLE="true">
<entry ENABLE="true" align="0 left">
<text ENABLE="true">
<DATA ENC="enc" LABEL="label">Second row, first column (entry)</DATA>
</text>
</entry>
<entry ENABLE="true" align="0 left">
<text ENABLE="true">
<DATA ENC="enc" LABEL="label">Second row, second column (entry)</DATA>
</text>
</entry>
</row>
</tbody>
</tgroup>
</table>
xsl fo可以是:
<xsl:template match="table">
<fo:block>
<fo:inline font-size="10pt" font-weight="bold" clear="both" float="right">
<xsl:text>Table </xsl:text><xsl:if test="@id"><xsl:apply-templates select="@id"/></xsl:if><xsl:if test="@title"><xsl:text> </xsl:text><xsl:apply-templates select="@title"/></xsl:if>
</fo:inline>
</fo:block>
<xsl:if test="descendant::*[self::row]">
<fo:table margin-top="0.2in" border-top-color="#a0c0ff" border-top-width="0.01042in" border-bottom-style="solid" border-bottom-color="#a0c0ff" border-bottom-width="0.01042in" border-left-style="solid" border-left-color="#a0c0ff" border-left-width="0.01042in" border-right-style="solid" border-right-color="#a0c0ff" border-right-width="0.01042in" border-collapse="separate" background-color="#e9e9ff" color="black" display-align="center" text-align="left">
<xsl:apply-templates select="child::*[self::thead or self::tbody or tfoot]" mode="calculate_columns"/>
<fo:table-body>
<xsl:apply-templates/>
</fo:table-body>
</fo:table>
<fo:block>
<fo:leader leader-pattern="space"/>
</fo:block>
</xsl:if>
</xsl:template>
<xsl:template match="row" mode="calculate_columns">
<xsl:variable name="columns" select="count(entry)"/>
<xsl:variable name="table-width" select="ancestor::table[@width]"/>
<xsl:param name="maxwidth" select="7.30000"/>
<xsl:variable name="column-width">
<xsl:value-of select="concat($table-width div $columns, 'cm')"/>
</xsl:variable>
<xsl:for-each select="entry">
<xsl:variable name="column-number">
<xsl:number level="multiple" count="entry" format="1" />
</xsl:variable>
<fo:table-column column-number="{$column-number}" column-width="{$column-width}"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="row">
<fo:table-row>
<xsl:apply-templates/>
</fo:table-row>
</xsl:template>
<xsl:template match="entry">
<fo:table-cell border-top-style="solid" border-top-color="#a0c0ff" border-top-width="0.01042in" border-bottom-style="solid" border-bottom-color="#a0c0ff" border-bottom-width="0.01042in" border-left-style="solid" border-left-color="#a0c0ff" border-left-width="0.01042in" border-right-style="solid" border-right-color="#a0c0ff" border-right-width="0.01042in">
<fo:block padding-top="1pt" padding-bottom="1pt">
<fo:inline>
<xsl:apply-templates/>
</fo:inline>
</fo:block>
</fo:table-cell>
</xsl:template>
上面的问题缺少xsl文本,这里是:xsl-FO中的表结构在这里定义:。与只输出fo:block和fo:inline元素不同,您必须使用fo:table、fo:table column等等。你到底有什么问题?