Creating Tables

Creating Tables

Some of the most useful constructs you can format with XSL-FO are tables. A table in XSL-FO is much like one in HTML: a rectangular grid of rows and columns of cells . You can use nine formatting elements to create tables:

  • <fo:table-and-caption>

  • <fo:table>

  • <fo:table-column>

  • <fo:table-caption>

  • <fo:table-header>

  • <fo:table-footer>

  • <fo:table-body>

  • <fo:table-row>

  • <fo:table- cell >

Creating tables in XSL-FO is similar to creating tables in HTML. You create an <fo:table> element to enclose the entire table, then format each column with an <fo:table-column> element. Next, create an <fo:table-body> element to specify the tables body. The <fo:table-body> element encloses all the <fo:table-row> elements, each of which creates a row in the table. Each <fo:table-row> element encloses the actual <fo:table-cell> elements that hold the tables cell-by-cell data.

The following example shows how this works. This XSLT stylesheet transforms planets.xml into an XSL-FO document that formats the planetary data in an XSL-FO-based table:

Listing 11.4 tables.xsl
 <?xml version="1.0"?>  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:fo="http://www.w3.org/1999/XSL/Format"      version="1.0">      <xsl:template match="PLANETS">          <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">              <fo:layout-master-set>                   <fo:simple-page-master master-name="page"                       page-height="400mm" page-width="300mm"                       margin-top="10mm" margin-bottom="10mm"                       margin-left="20mm" margin-right="20mm">                       <fo:region-body                         margin-top="0mm" margin-bottom="10mm"                         margin-left="0mm" margin-right="0mm"/>                       <fo:region-after extent="10mm"/>                   </fo:simple-page-master>              </fo:layout-master-set>              <fo:page-sequence master-name="page">                  <fo:flow flow-name="xsl-region-body">                      <fo:table>                          <fo:table-column column-width="30mm"/>                          <fo:table-column column-width="30mm"/>                          <fo:table-column column-width="30mm"/>                          <fo:table-column column-width="30mm"/>                          <fo:table-column column-width="30mm"/>                          <fo:table-column column-width="30mm"/>                          <fo:table-body>                              <fo:table-row>                                  <fo:table-cell border-width="0.5mm">                                      <fo:block font-size="18pt"                                          font-weight="bold">                                          Name                                      </fo:block>                                  </fo:table-cell>                                  <fo:table-cell border-width="0.5mm">                                        <fo:block font-size="18pt"                                            font-weight="bold">                                            Mass                                        </fo:block>                                    </fo:table-cell>                                    <fo:table-cell border-width="0.5mm">                                        <fo:block font-size="18pt"                                            font-weight="bold">                                            Day                                        </fo:block>                                    </fo:table-cell>                                    <fo:table-cell border-width="0.5mm">                                        <fo:block font-size="18pt"                                            font-weight="bold">                                            Radius                                        </fo:block>                                    </fo:table-cell>                                    <fo:table-cell border-width="0.5mm">                                        <fo:block font-size="18pt"                                            font-weight="bold">                                            Density                                        </fo:block>                                    </fo:table-cell>                                    <fo:table-cell border-width="0.5mm">                                        <fo:block font-size="18pt"                                            font-weight="bold">                                            Distance                                        </fo:block>                                    </fo:table-cell>                                </fo:table-row>                                <xsl:apply-templates/>                            </fo:table-body>                        </fo:table>                   </fo:flow>               </fo:page-sequence>          </fo:root>      </xsl:template>      <xsl:template match="PLANET">          <fo:table-row>              <xsl:apply-templates/>          </fo:table-row>      </xsl:template>      <xsl:template match="NAME">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>      <xsl:template match="MASS">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>      <xsl:template match="DAY">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>      <xsl:template match="RADIUS">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>      <xsl:template match="DENSITY">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>      <xsl:template match="DISTANCE">            <fo:table-cell border-width="0.5mm">                <fo:block font-size="18pt">                    <xsl:value-of select='.'/>                </fo:block>            </fo:table-cell>      </xsl:template>  </xsl:stylesheet> 

Heres the result after you transform to an XSL-FO document, tables.fo:

Listing 11.5 tables.fo
 <?xml version="1.0" encoding="UTF-8"?>  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">      <fo:layout-master-set>          <fo:simple-page-master margin-right="20mm" margin-left="20mm"              margin-bottom="10mm" margin-top="10mm" page-width="300mm"              page-height="400mm" master-name="page">              <fo:region-body margin-right="0mm" margin-left="0mm"                  margin-bottom="10mm" margin-top="0mm"/>              <fo:region-after extent="10mm"/>          </fo:simple-page-master>      </fo:layout-master-set>      <fo:page-sequence master-name="page">          <fo:flow flow-name="xsl-region-body":>              <fo:table>                  <fo:table-column column-width="30mm"/>                  <fo:table-column column-width="30mm"/>                  <fo:table-column column-width="30mm"/>                  <fo:table-column column-width="30mm"/>                  <fo:table-column column-width="30mm"/>                  <fo:table-column column-width="30mm"/>                  <fo:table-body>                      <fo:table-row>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-weight="bold" font-size="18pt">                                  Name                              </fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-weight="bold" font-size="18pt">                                  Mass                              </fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-weight="bold" font-size="18pt">                                  Day                              </fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-weight="bold" font-size="18pt">                                                 Radius                                             </fo:block>                                         </fo:table-cell>                                             <fo:table-cell border-width="0.5mm">                                                 <fo:block font-weight="bold"                                                     font-size="18pt">                                                     Density                                                 </fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-weight="bold" font-size="18pt">                                                 Distance                                             </fo:block>                                         </fo:table-cell>                                     </fo:table-row>                                     <fo:table-row>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">Mercury</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">.0553</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">58.65</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">1516</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">.983</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">43.4</fo:block>                                         </fo:table-cell>                                     </fo:table-row>                                     <fo:table-row>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">Venus</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">.815</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">116.75</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                                             <fo:block font-size="18pt">3716</fo:block>                                         </fo:table-cell>                                         <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">.943</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">66.8</fo:block>                          </fo:table-cell>                      </fo:table-row>                      <fo:table-row>                          <fo:table-cell border-width="0.5mm">                               <fo:block font-size="18pt">Earth</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                               <fo:block font-size="18pt">1</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">1</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">2107</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">1</fo:block>                          </fo:table-cell>                          <fo:table-cell border-width="0.5mm">                              <fo:block font-size="18pt">128.4</fo:block>                          </fo:table-cell>                      </fo:table-row>                  </fo:table-body>              </fo:table>          </fo:flow>      </fo:page-sequence>  </fo:root> 

After running this document, tables.fo, through fop and creating tables.pdf, you can see the result in Figure 11.2. Thats what an XSL-FO table looks like, although there are plenty of other optionsyou can set the background color cell by cell with the background-color property, for example. By default no border appears in these tables, but Ive added one 0.5 mm thick with the border-width property. Note also that to set the font size of the text in each cell, Im using a block inside each table cell:

Figure 11.2. An XSL-FO formatted table in Adobe Acrobat.
graphics/11fig02.gif
 <fo:table-cell border-width="0.5mm">      <fo:block font-size="18pt">Earth</fo:block>  </fo:table-cell> 

Ill take a look at the various elements you use to create tables now, starting with the big one, <fo:table> .



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net