Re: [xsl] ordering and iteration problem

Subject: Re: [xsl] ordering and iteration problem
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Apr 2001 10:47:24 +0100
Hi Mark,

> My thinking is that I need to do something like
>    for each row
>        for each column
>            ooutput the <circuit-breaker> with that row and column

I'd probably do this using the Piez Method/Hack of having an
xsl:for-each iterate over the correct number of random nodes and using
the position of the node to give the row/column number.

You need to define some random nodes - I usually use nodes from the

<xsl:variable name="random-nodes" select="document('')//node()" />

And since you'll be iterating over them, you need some way of getting
back to the data:

<xsl:variable name="data" select="/" />

I've used two keys to get the relevant circuit breakers quickly. One
just indexes them by column (this is so you can work out whether you
need to add a cell or whether there's a circuit breaker from higher in
the column that covers it). The other indexes them by row and column.

<xsl:key name="breakers-by-column" match="b:circuit-breaker"
         use="@column" />
<xsl:key name="breakers" match="b:circuit-breaker"
         use="concat(@row, ':', @column)" />

I've assumed that you've stored the maximum number of rows in a
global variable called $max-row and the maximum number of columns in a
global variable called $max-col.  Here's the template that does the
<xsl:template match="/">
   <!-- store the right number of nodes for the rows in a variable -->
   <xsl:variable name="rows"
                 select="$random-nodes[position() &lt;= $max-row]" />
   <!-- store the right number of nodes for the columns in a variable
   <xsl:variable name="columns"
                 select="$random-nodes[position() &lt;= $max-col]" />
   <!-- create the table -->
      <!-- iterate over the right set of nodes to get the rows -->
      <xsl:for-each select="$rows">
         <!-- store the row number -->
         <xsl:variable name="row" select="position()" />
         <!-- create the row -->
            <!-- iterate over the right set of nodes to get the
                 columns -->
            <xsl:for-each select="$columns">
               <!-- store the column number -->
               <xsl:variable name="col" select="position()" />
               <!-- change the current node so that the key works -->
               <xsl:for-each select="$data">
                  <!-- identify the relevant circuit breaker -->
                  <xsl:variable name="breaker"
                                            concat($row, ':', $col))" />
                     <!-- if there is one, apply templates to get the
                          table cell -->
                     <xsl:when test="$breaker">
                        <xsl:apply-templates select="$breaker" />
                        <!-- find other breakers that start higher in
                             the column -->
                        <xsl:variable name="column-breakers"
                                      select="key('breakers-by-column', $col)
                                                 [@row &lt; $row]" />
                        <!-- output an empty cell if there isn't one
                             that overlaps -->
                        <xsl:if test="not($column-breakers
                                             [@row + @height &gt; $row])">
                           <td />

<!-- template to give the cell for the circuit breaker -->
<xsl:template match="b:circuit-breaker">
   <td rowspan="{@height}">
      <xsl:value-of select="b:amps" />

If you don't like the 'one big template' approach, then you could
split it down by applying templates to the row and column nodes in
'row' and 'column' modes to distinguish between the two.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread