xsl -xml fo文件-将表数据输入到xml

问题描述

|| 我有xsl-xml文件,可以将其转换为fop文件(使用FOP factory,
javax.xml.transform.TransformerFactory
),因此最终我可以将其转换为PDF。到现在为止,我只需要输入简单的数据,因此在xsl中,我将编写如下内容
<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中添加什么?     

解决方法

我不明白您想要什么...但是在每种情况下,我都在写一个表格示例。 假设您有一个表格,其格式如下,其中标记“ \”行代表表的行,标记“ \ entry \”代表列:
  <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&#x20;</xsl:text><xsl:if test=\"@id\"><xsl:apply-templates select=\"@id\"/></xsl:if><xsl:if test=\"@title\"><xsl:text>&#x20;</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>