Search code examples
javaxmlpdfxsl-foapache-fop

How to repeat same template in table-row using XSL-FO/Apache FOP


I'm trying to create a PDF from an XML file I've got and it's working fairly well so far using XSL-FO/Apache FOP.

The XML file basically contains barcode information: the barcode itself and the barcode type (I'll add the barcode image at some point as well).

Now what I'd like to see as the output is this:

 -----------------------
| barcode1  | barcode2  |
| codetype1 | codetype2 |
 -----------------------
| barcode3  | barcode4  |
| codetype3 | codetype4 |
 -----------------------

And so on.

I've defined the following xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
  <xsl:template match="barcode-list">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">
        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="10pt">
          <fo:table table-layout="fixed" width="100%" border-collapse="separate">    
            <fo:table-column column-width="45%"/>
            <fo:table-column column-width="45%"/>
            <fo:table-body>
              <xsl:apply-templates select="item"/>
            </fo:table-body>
          </fo:table>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
     </fo:root>
  </xsl:template>
  <xsl:template match="item">
    <fo:table-row>   
      <fo:table-cell>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="name"/>
        </fo:block>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="format"/>
        </fo:block>
      </fo:table-cell> 
    </fo:table-row>
  </xsl:template>
</xsl:stylesheet>

So there are two columns and I assumed I could just point it at the "item" template to fill in the cells.

Now I realize that the "item" template contains a <table-row> tag and that that causes each item to appear in its own table row. So what I get is this:

 -----------------------
| barcode1  |           |
| codetype1 |           |
 -----------------------
| barcode2  |           |
| codetype2 |           |
 -----------------------
| barcode3  |           |
| codetype3 |           |
 -----------------------
| barcode4  |           |
| codetype4 |           |
 -----------------------

My question is how to change the xsl to get the desired output rather than getting each item in its own table row?


Solution

  • Omit the fo:table-row and use the rarely-used starts-row (https://www.w3.org/TR/xsl11/#starts-row) and/or ends-row (https://www.w3.org/TR/xsl11/#ends-row) properties:

    <xsl:template match="item">
      <fo:table-cell>
        <xsl:if test="position() mod 2 = 1">
          <xsl:attribute name="starts-row">true</xsl:attribute>
        </xsl:if>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="name"/>
        </fo:block>
        <fo:block wrap-option="wrap">
          <xsl:value-of select="format"/>
        </fo:block>
      </fo:table-cell> 
    </xsl:template>