Re: [xsl] FO/XSL:Setting up columns in a for-each loop

Subject: Re: [xsl] FO/XSL:Setting up columns in a for-each loop
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 31 Oct 2001 18:19:58 +0000
Hi Rachael,

> Thanks so much for the response. Do you happen to know where I can
> find any FO examples of this type of logic. I am trying this example
> and others, but I keep getting errors. I think my placements are
> wrong.

Here's an example. The source is as follows:

<list>
  <item>item1</item>
  <item>item2</item>
  ...
  <item>item3</item>
</list>

And you want to create a four-column table. The list equates to an
fo:table:

<xsl:template match="list">
  <fo:table>
    <fo:table-body>
      ...
    </fo:table-body>
  </fo:table>
</xsl:template>

Now, you want to generate a row for every fourth item in the list. You
can get every fourth item by seeing if its position mod 4 is equal to
1 with:

  position() mod 4 = 1

You can iterate over every fourth item with an xsl:for-each (or you
can apply templates to them) as follows:

<xsl:template match="list">
  <fo:table>
    <fo:table-body>
      <xsl:for-each select="item[position() mod 4 = 1]">
        <fo:table-row>
          ...
        </fo:table-row>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</xsl:template>

Now you need to create a cell for the current item and its three
following siblings. You can get the current item with . and its three
following siblings with:

  following-sibling::item[position() &lt; 4]

And you can iterate over these with xsl:for-each again (or
xsl:apply-templates if you prefer):

<xsl:template match="list">
  <fo:table>
    <fo:table-body>
      <xsl:for-each select="item[position() mod 4 = 1]">
        <fo:table-row>
          <xsl:for-each
              select=". | following-sibling::item[position() &lt; 4]">
            <fo:table-cell>
              <xsl:value-of select="." />
            </fo:table-cell>
          </xsl:for-each>
        </fo:table-row>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</xsl:template>

If that example doesn't help, you could always send the part of the
stylesheet that's causing problems and we can see if we can spot where
it should be changed.

I hope that helps anyway,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread