xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Dupras <bri...@centera.com>
Subject RE: simple (?) xsl question
Date Wed, 09 Feb 2000 16:12:43 GMT
Mike - I can't thank you enough for your great example.  I made some changes
to work in my exact setup.  For the sake of others who'll likely be
searching the archives of this list in 8 months, here's my final solution:

<xsl:template match="pp:data/pp:page">
		<xsl:variable name="cols" select="4"/>
		<html:table width="100%" cellpadding="5" border="0">
				<xsl:for-each select="pp:page[(position() -
1) mod $cols = 0]">
					<xsl:variable name="iRow"
select="(position() - 1) mod $cols"/>
					<html:tr><xsl:attribute
name="bgcolor"></xsl:attribute>
						<xsl:for-each
select="self::* | following::*[position() &lt; $cols]">
							<xsl:variable
name="iCol" select="position()"/>
							<html:td
align="left"><html:a class="SiteBranch"><xsl:attribute
name="href"><xsl:value-of select="@rid"/></xsl:attribute><xsl:value-of
select="@description"/></html:a></html:td>

							<!-- EMPTY TABLE
CELLS: are we on the last row? are there cells remaining to fill in? -->
							<xsl:if
test="position() = last()">
								<xsl:if
test="$cols - position() &gt; 0">
									<!--
fill in remaining cells -->
	
<xsl:call-template name="filler_cells">
	
<xsl:with-param name="cell_count" select="($cols - position())"/>
	
</xsl:call-template>
								</xsl:if>
							</xsl:if>

						</xsl:for-each>
					</html:tr>
				</xsl:for-each>
		</html:table>
</xsl:template>

<xsl:template name="filler_cells">
  <xsl:param name="cell_count"/>
  <xsl:if test="$cell_count">
    <html:td>&#160;<!-- xsl:value-of select="$cell_count"/ --></html:td>
    <xsl:call-template name="filler_cells">
      <xsl:with-param name="cell_count" select="$cell_count - 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>


Thanks again

Brian


> -----Original Message-----
> From: Mike Brown [mailto:mike@skew.org]
> Sent: Tuesday, February 08, 2000 9:12 PM
> To: general@xml.apache.org
> Cc: briand@centera.com
> Subject: Re: simple (?) xsl question
> 
> 
> Nevertheless, it's an interesting challenge to come up with a generic
> solution to this common problem.
> 
> > Let's say I'm doing an <xsl:for-each ...> over an unknown 
> number of xml
> > elements.  I want to format these elements to an HTML table 
> that 4 columns
> > wide.  Inside my <xsl:for-each ...>, how do I say "every 
> fourth iteration,
> > close the <html:tr> and open another <html:tr>"?
> 
> You don't. You need to change your approach to building 
> tables. Rather than
> thinking in terms of start tags and end tags, you must think 
> in terms of
> complete elements; assemble an entire table, table row, or 
> table cell at
> once.
> 

[nicely done example snipped for brevety]

> 
> 
> I'm not sure this is the shortest solution, but it seems to work.
> 
>    - Mike
> ______________________________________________________________
> __________
>  Mike Brown / Hyperreal   |  Hyperreal http://www.hyperreal.org/music/
>  PO Box 61334             |     XML & XSL http://www.skew.org/xml/
>  Denver CO 80206-8334 USA |       http://www.hyperreal.org/~mike/
> 

Mime
View raw message