Search code examples
xmlpdfxslthtml-tablexsl-fo

xsl-fo table creation from xml


I have the following xml format for tables we want to build that works fine for the way is loop over the structure. However I cannot figure out out to do it for xsl-fo transforming to PDF.

 <table>
      <thead>
           <tcell>data</tcell>
           <tcell>data</tcell>
           <tcell>data</tcell>
      </thead>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
 </table>

Solution

  • You don't have much of a question (just some vague requirements), but this should at least get you started.

    XML Input

    <table>
        <thead>
            <tcell>data</tcell>
            <tcell>data</tcell>
            <tcell>data</tcell>
        </thead>
        <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
        <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
        <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
        <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
    </table>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/">
            <fo:root>
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                        <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="my-page">
                    <fo:flow flow-name="xsl-region-body"> 
                        <xsl:apply-templates/>
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>        
        </xsl:template>
    
        <xsl:template match="table">
            <fo:table>
                <xsl:apply-templates select="thead"/>
                <fo:table-body>
                    <xsl:apply-templates select="trow"/>
                </fo:table-body>
            </fo:table>
        </xsl:template>
    
        <xsl:attribute-set name="trow">
            <xsl:attribute name="border">solid</xsl:attribute>
            <xsl:attribute name="padding-left">1em</xsl:attribute>
            <xsl:attribute name="padding-right">1em</xsl:attribute>
        </xsl:attribute-set>
    
        <xsl:template match="thead">
            <fo:table-header>
                <fo:table-row background-color="#FFDEAD" text-align="center">
                    <xsl:apply-templates/>
                </fo:table-row>
            </fo:table-header>
        </xsl:template>
    
        <xsl:template match="tcell">
            <fo:table-cell xsl:use-attribute-sets="trow">
                <fo:block><xsl:value-of select="."/></fo:block>
            </fo:table-cell>
        </xsl:template>
    
        <xsl:template match="trow">
            <fo:table-row>
                <xsl:apply-templates/>
            </fo:table-row>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XSL-FO Output (XSLT processor used: Saxon-HE)

    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:layout-master-set>
          <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
             <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
          </fo:simple-page-master>
       </fo:layout-master-set>
       <fo:page-sequence master-reference="my-page">
          <fo:flow flow-name="xsl-region-body">
             <fo:table>
                <fo:table-header>
                   <fo:table-row background-color="#FFDEAD" text-align="center">
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                   </fo:table-row>
                </fo:table-header>
                <fo:table-body>
                   <fo:table-row>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                   </fo:table-row>
                   <fo:table-row>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                   </fo:table-row>
                   <fo:table-row>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                   </fo:table-row>
                   <fo:table-row>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                      <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                         <fo:block>data</fo:block>
                      </fo:table-cell>
                   </fo:table-row>
                </fo:table-body>
             </fo:table>
          </fo:flow>
       </fo:page-sequence>
    </fo:root>
    

    Rendered PDF (XSL-FO processor used: Apache FOP)

    enter image description here