Re: [xsl] mod position() tests positive all of the time

Subject: Re: [xsl] mod position() tests positive all of the time
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Sat, 30 Dec 2006 14:57:49 +0100
Hi Allen,

I had some additional thoughts, see below



Abel Braaksma wrote:

Why did you choose to use call-template? Using apply-template and template matches is likely much easier.


<xsl:template name="results">
<table>
<xsl:for-each
select="insightResponse/searchResponse/collectionResultSet/object/thumbnail[position()


mod 5 = 1]">
What this does highly depends on your input. For instance, if you have a set of "object" nodes, and each has one "thumbnail" node, then this for-each will select all of them (as postion() is always for each first thumbnail node under object). Depending on your input, you may want to write this a little different for readability, but as far as I can tell, this code is not wrong per se.




Because it looks as if you want to output several thumbnails of pictures where you specify the row-size and you use XSLT for the logic, here's is a generalized example of what you can use by using templates (instead of for-each). Note the use of modes, it makes it easier to apply a template multiple times to an input node, without getting into infinite recursion.


Input file:
<input>
   <thumbnail url="http://someurl/bla1.gif"; />
   <thumbnail url="http://someurl/bla2.gif"; />
   <thumbnail url="http://someurl/bla3.gif"; />
   <thumbnail url="http://someurl/bla4.gif"; />
   <thumbnail url="http://someurl/bla5.gif"; />
   <thumbnail url="http://someurl/bla6.gif"; />
   <thumbnail url="http://someurl/bla7.gif"; />
   <thumbnail url="http://someurl/bla8.gif"; />
   <thumbnail url="http://someurl/bla9.gif"; />
   <thumbnail url="http://someurl/bla10.gif"; />
   <thumbnail url="http://someurl/bla11.gif"; />
   <thumbnail url="http://someurl/bla12.gif"; />
   <thumbnail url="http://someurl/bla13.gif"; />
   <thumbnail url="http://someurl/bla14.gif"; />
</input>

XSLT file:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>


<xsl:template match="/input">
<html><body>
<table>
<xsl:apply-templates select="thumbnail" />
</table>
</body></html>
</xsl:template>
<xsl:template match="thumbnail[position() mod 5 = 1]">
<xsl:variable name="pos" select="position()" />
<tr>
<xsl:apply-templates
mode="makethumb"
select=". |
following-sibling::thumbnail[position() &lt; 5]"
/> </tr>
</xsl:template>
<xsl:template match="thumbnail" mode="makethumb">
<td>
<img src="{@url}" />
</td>
</xsl:template>
</xsl:stylesheet>



Output after transformation: <html> <body> <table> <tr> <td><img src="http://someurl/bla1.gif"/></td> <td><img src="http://someurl/bla2.gif"/></td> <td><img src="http://someurl/bla3.gif"/></td> <td><img src="http://someurl/bla4.gif"/></td> <td><img src="http://someurl/bla5.gif"/></td> </tr> <tr> <td><img src="http://someurl/bla6.gif"/></td>* * <td><img src="http://someurl/bla7.gif"/></td> <td><img src="http://someurl/bla8.gif"/></td> [.....] etc.


Cheers, -- Abel Braaksma http://www.nuntia.nl

Current Thread