[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: Re: mod position() tests positive all of the time
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Sat, 30 Dec 2006 14:57:49 +0100
xsl td mod
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Cast Your Vote

We need your help – Vote for DataDirect XML Products!

  • Best SOA or XML site

Winners and finalists announced at SOA World Conference in November.

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.