Re: [xsl] math get maximum and subtract 1

Subject: Re: [xsl] math get maximum and subtract 1
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 24 Jul 2002 18:27:53 +0100
Hi Sascha,

> this is my whole input:
>
> <DESCRIPTION_TABLE>
>    <DESCRIPTION_ITEM col="0" row="0">
>     <LABEL>Verantwortlicher</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="0" row="1">
>     <LABEL>Prozessziel</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="0" row="2">
>     <LABEL>Messgrösse</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="0" row="3">
>     <LABEL>Benötigte Informationen</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="0" row="4">
>     <LABEL>Erzeugte Ergebnisse</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="1" row="0">
>     <LABEL>Externe Vorschriften</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="1" row="1">
>     <LABEL>Hinweise</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="1" row="2">
>     <LABEL>Potenzial</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="1" row="3">
>     <LABEL>Mitgeltende Unterlagen</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>    <DESCRIPTION_ITEM col="1" row="4">
>     <LABEL>Offene Fragen</LABEL>
>     <VALUE>Text</VALUE>
>    </DESCRIPTION_ITEM>
>   </DESCRIPTION_TABLE>

If your XML is organised like this (i.e. sorted so that the last cell
in the table is the last DESCRIPTION_ITEM in the DESCRIPTION_TABLE),
then it's not too difficult to get a count of the columns and rows,
actually -- just look at col and row attributes on the last
DESCRIPTION_ITEM within the DESCRIPTION_TABLE. The number of columns
is:

  DESCRIPTION_TABLE/DESCRIPTION_ITEM[last()]/@col + 1

and the number of rows is:

  DESCRIPTION_TABLE/DESCRIPTION_ITEM[last()]/@row + 1

> i need to make a table with 2 cols and 5 rows but there is no FOR -
> loop in XSL, right, so i have to use a xsl:for-each , i guess... but
> how?
>
> output is a <fo:table>

Then it's *much* easier than you think. fo:table doesn't require you
to group your cells into rows -- instead, you can use the starts-row
property on those cells that start a row.

First, the DESCRIPTION_TABLE maps onto your fo:table:

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

Then you iterate over the DESCRIPTION_ITEMs in order to create the
cells. The DESCRIPTION_ITEMs need to be sorted by row, and by column
within the row, as follows:

<xsl:template match="DESCRIPTION_TABLE">
  <fo:table>
    <fo:table-body>
      <xsl:for-each select="DESCRIPTION_ITEM">
        <xsl:sort select="@row" data-type="number" />
        <xsl:sort select="@col" data-type="number" />
        <fo:cell>
          ...
          <xsl:apply-templates />
        </fo:cell>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</xsl:template>

and if the DESCRIPTION_ITEM starts a row (i.e. its column number is 0)
then you add a 'starts-row' attribute with the value 'true':

<xsl:template match="DESCRIPTION_TABLE">
  <fo:table>
    <fo:table-body>
      <xsl:for-each select="DESCRIPTION_ITEM">
        <xsl:sort select="@row" data-type="number" />
        <xsl:sort select="@col" data-type="number" />
        <fo:cell>
          <xsl:if test="@col = 0">
            <xsl:attribute name="starts-row">true</xsl:attribute>
          </xsl:if>
          <xsl:apply-templates />
        </fo:cell>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</xsl:template>

If you were having to group the cells into rows then it would be a bit
more complicated -- you'd have to use a grouping method. It would also
be trickier if you had missing cells.

Cheers,

Jeni

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


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


Current Thread