This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Counting Colums in WML
- To: "Ciaran Byrne" <ciaran dot byrne at 3tl dot com>
- Subject: Re: [xsl] Counting Colums in WML
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Wed, 28 Feb 2001 19:21:20 +0000
- CC: "Xsl-List" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <NEBBIEMMKMAEPLDEIMPEMEDMCFAA.ciaran.byrne@3tl.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Ciaran,
> I'm attempting to calculate the number of colums
> as the no. of columns required by the row with the most columns.
>
> So in my stylesheet I have something along the lines of
>
> <xsl:for-each select="tr">
> <xsl:choose>
> <xsl:when test="count(td) > count(preceding::td)">
> <xsl:value-of select="count(td)"/>
> </xsl:when>
> </xsl:choose>
> </xsl:for-each>
>
> How do I store the max. value ? In a param/ variable ?
You can get the maximum just with an xsl:for-each as above: go through
the table rows, using the number of table cells they contain to
organise them in descending order. Then pick off the first one:
<xsl:for-each select="tr">
<xsl:sort select="count(td)" data-type="number"
order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="count(td)" />
</xsl:if>
</xsl:for-each>
If you're dealing with really big tables, then you might want to use
recursion instead. You need to create a template that matches a table
row. If it can find another row that has more cells than it does,
then apply templates to it. If there isn't one, then give the value
of the number of cells in this row.
<xsl:template match="tr" mode="max">
<xsl:variable name="next-larger"
select="following-sibling::tr
[count(td) > count(current()/td)][1]" />
<xsl:choose>
<xsl:when test="$next-larger">
<xsl:apply-templates select="$next" mode="max" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(td)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list