2
votes

Good day, please advise. I have a list of items:

<List>
  <Item>
    <Description>Item 1</Description>
    <Code>0001</Code>
  </Item>
  <Item>
    <Description>Item 2</Description>
    <Code>0002</Code>
  </Item>
  <Item>
    <Description>Item 3</Description>
    <Code>0003</Code>
  </Item>
  <Item>
    <Description>Item 4</Description>
    <Code>0004</Code>
  </Item>
</List>

I'd like to print a 3-column table containing these items like this:

| Column 1 | Column 2 | Column 3 |
==================================
| Item 1   | Item 2   | Item 3   |
----------------------------------
| Item 4   |          |          |
----------------------------------

Code below renders table cell for each item. It does not render cells for column 2 and 3 in the second row:

<fo:table>
  <fo:table-column width="82mm"/>
  <fo:table-column width="82mm"/>
  <fo:table-column width="82mm"/>
  <fo:table-body>
    <xsl:for-each select="/List/Item[position() mod 3 = 1]">
      <fo:table-row><xsl:apply-templates select=". | following-sibling::*[3 > position()]"/></fo:table-row>
    </xsl:for-each>
  </fo:table-body>
</fo:table>

<xsl:template match="Item">
  <fo:table-cell><fo:block><xsl:value-of select="Description"/></fo:block></fo:table-cell>
</xsl:template>

The table is then printed like this:

| Column 1 | Column 2 | Column 3 |
==================================
| Item 1   | Item 2   | Item 3   |
----------------------------------
| Item 4   |
------------

Is there an elegant way how render even empty table cells so that table appears full? I have to use FOP 0.95, xsl version 1.0.

Thank you in advance

Vojtech

1

1 Answers

1
votes

Hope this will give you an idea of a possible solution:

Let's have this simple XML document :

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

then this transformation:

<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 omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
        <fo:table>
            <fo:table-column width="82mm"/>
            <fo:table-column width="82mm"/>
            <fo:table-column width="82mm"/>
            <fo:table-body>
               <xsl:for-each select="*[position() mod 3 = 1]">
                 <fo:table-row>
                   <xsl:apply-templates select=
                                  ". | following-sibling::*[3 > position()]"/>

                        <xsl:variable name="vPos" select="position()"/>
                        <xsl:variable name="vUnfilled"
                             select=" 2 - count(following-sibling::*)"/>
                        <xsl:if test="position() = last()">
                         <xsl:for-each select=
                                   "../*[not(position() > $vUnfilled)]">
                            <fo:table-cell>
                                <fo:block>
                                    <xsl:value-of select="' '"/>
                                </fo:block>
                            </fo:table-cell>
                         </xsl:for-each>
                        </xsl:if>
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>
        </fo:table>
    </xsl:template>

    <xsl:template match="num">
        <fo:table-cell>
            <fo:block>
                <xsl:value-of select="."/>
            </fo:block>
        </fo:table-cell>
    </xsl:template>
</xsl:stylesheet>

when applied on the above XML document, produces a table with the wanted full number of cells for each row:

<fo:table xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:table-column width="82mm"/>
   <fo:table-column width="82mm"/>
   <fo:table-column width="82mm"/>
   <fo:table-body>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>01</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>02</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>03</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>04</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>05</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>06</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>07</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>08</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block>09</fo:block>
         </fo:table-cell>
      </fo:table-row>
      <fo:table-row>
         <fo:table-cell>
            <fo:block>10</fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block> </fo:block>
         </fo:table-cell>
         <fo:table-cell>
            <fo:block> </fo:block>
         </fo:table-cell>
      </fo:table-row>
   </fo:table-body>
</fo:table>