This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Finding the maximum number of nodes (Redux)
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] Finding the maximum number of nodes (Redux)
- From: "Pepping, Simon (ELS)" <S dot Pepping at elsevier dot nl>
- Date: Mon, 15 Oct 2001 09:51:55 +0200
- Reply-To: xsl-list at lists dot mulberrytech dot com
Marty McKeever wrote:
> So here is my XPath solution, submitted for comment.
>
> The XML:
>
> <table>
> <tr><td>1.1</td><td>1.2</td></tr>
> <tr><td>2.1</td><td>2.2</td><td>2.3</td></tr>
> <tr><td>3.1</td></tr>
> </table>
>
> The Desired Result:
>
> <table>
> <tr><td colspan="3">Table Header Text</td></tr>
> <tr><td>1.1</td><td>1.2</td></tr>
> <tr><td>2.1</td><td>2.2</td><td>2.3</td></tr>
> <tr><td>3.1</td></tr>
> </table>
>
> The XSLT:
>
> <xsl:template match="table">
> <table border="1"><tr><td>
> <xsl:attribute name="colspan"><xsl:value-of
> select="count(tr[count(td) >
> count(following-sibling::tr[count(td) >
> count(preceding-sibling::tr[last()]/td)]/td)][count(td) >
> count(preceding-sibling::tr[last()]/td)]/td)"/></xsl:attribute>
> Table Header Text</td></tr>
> <xsl:copy-of select="tr"/>
> </table>
> </xsl:template>
For this table it does not work:
<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></tr>
<tr><td>4.1</td><td>4.2</td><td>4.3</td></tr>
<tr><td>5.1</td><td>5.2</td></tr>
</table>
This is the resulting html:
<table border="1">
<tr><td colspan="5">Table Header Text</td></tr>
<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></tr>
<tr><td>4.1</td><td>4.2</td><td>4.3</td></tr>
<tr><td>5.1</td><td>5.2</td></tr>
</table>
This is an account of what the stylesheet does:
1
1 2 3
1
1 2 3
1 2
count(tr[count(td) >
count(following-sibling::tr[count(td) >
count(preceding-sibling::tr[last()]/td)]/td)][count(td) >
count(preceding-sibling::tr[last()]/td)]/td)
1: 1 >
2: 3 > 1
3: 1 > 1 no
4: 3 > 1
5: 2 > 1
-----------
3 + 3 + 2 = 6 no
2: 3 >
3: 1 > 1 no
4: 3 > 1
5: 2 > 1
-----------
3 + 2 = 5 no
3: 1 >
4: 3 > 1
5: 2 > 1
-----------
3 + 2 = 5 no
4: 3 >
5: 2 > 1
-----------
2 yes
5: 2 >
-----------
0 yes
===
4: 3 > 1
5: 2 > 1
--------
3 + 2 = 5
It is a heuristic at best. count(preceding-sibling::tr[last()] is
always the first tr. following-sibling::tr[count(td) >
count(preceding-sibling::tr[last()]/td)] may contain several rows,
which does not seem a good idea in an algorithm that tries to find the
maximum number of cols. Similarly for the final set of tr.
Simon Pepping
Elsevier Science
s.pepping@elsevier.nl
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list