开发者

xsl -xml fo file - entering table data to the xml

I have xsl-xml files that I turn into fop file (using FOP factory, javax.xml.transform.TransformerFactory) so eventually I can turn it into a PDF. Until now I had to enter only simple data, so in the xsl I would write something like:

<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>

and so on.

And when I transform it into fop file I get:

<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>

etc...

开发者_运维技巧

Now I have a situation that I need to display a table with data. How do I define the table template in XML and what do I have to add in the XML?


I don't understand what you want...but in every case I'm writing an example of table. Suppose you have a table formatted as follow, where the tag "row" represent the rows of the table and the tag "entry" the columns:

  <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> 

The xsl-fo could be:

    <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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜