Re: [xsl] Finding the maximun number of nodes

Subject: Re: [xsl] Finding the maximun number of nodes
From: "Michael Lee" <mlee@xxxxxxxxxx>
Date: Fri, 5 Jan 2001 16:59:17 +0800
Thank you for your help.  However, this solution doesn't seem to solve the
problem if there is more than one table in the HTML file because using
<xsl:variable> requires us to know how many tables are there in advance, if
we are given the HTML files at process-time.

Dimitre's solution makes use of <xsl:key>, something that I am not familiar
with.  However, I believe it still doesn't work if the stylesheet has to
handle an unknown number of tables.

Michael Lee

----- Original Message -----
From: "Jiri Jirat" <Jiri.Jirat@xxxxxxxxx>

Hello Michael,
 here is the solution:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="";

<xsl:variable name="maximum">
 <xsl:apply-templates select="table" mode="howmany"/>

<xsl:template match="/">
 <table columns="{substring-before($maximum,'~')}">
  <!-- ...  -->

<xsl:template match="table" mode="howmany">
  <xsl:apply-templates select="tr" mode="howmany">
   <xsl:sort select="count(td)" order="descending" data-type="number"/>

<xsl:template match="tr" mode="howmany">
 <xsl:value-of select="count(td)"/>


<?xml version="1.0" encoding="utf-8"?>
<table columns="3"/>

Michael Lee wrote:

> I am writing a XSLT stylesheet to transform a simple table from HTML to
> WML. However, the "columns" attribute is required for the "table" element
> in the latter format.  Therefore, I must be able to determine the maximum
> number of cells in the rows and use it as the value for the "columns"
> attribute.
> The problem is that there is no way I can store and compare the result
> after I count the number of <td> in each <tr>.  I can do a sort and then
> count the first or last row, but then there is no way I can unsort the
> rows again to preserve the original output.
> Does anyone have any idea if this problem has a purely XSLT solution?  The
> following is my input and desired output:
> HTML input:
> <table>
>    <tr><td>(1,1)</td></tr>
>    <tr><td>(2,1)</td><td>(2,2)</td><td>(2,3)</td></tr>
>    <tr><td>(3,1)</td><td>(3,2)</td></tr>
> </table>
> WML output:
> <table columns="3">
>    <tr><td>(1,1)</td></tr>
>    <tr><td>(2,1)</td><td>(2,2)</td><td>(2,3)</td></tr>
>    <tr><td>(3,1)</td><td>(3,2)</td></tr>
> </table>

 XSL-List info and archive:

Current Thread