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: Tue, 02 Jan 2007 17:27:05 +0100
Allen Jones wrote:
I'm still having the same counting
problem after working with some of the suggestions.  I have worked
mainly with Abel's and Chuck Knell's suggestion, and I'm getting the
same results.  This is output that comes from an XML gateway into a
PHP/Sablotron script for formatting.

As per Abel's suggection, the input xml data I am using is included in
the post:

Thanks for your input. That clarifies a lot. Others have already pointed out the difference between the siblings and cousins of object and thumbnail nodes. Here's a solution that works with your input, using only templates and no xsl:for-each (push model) or xsl:choose (often, an xsl:choose can be replaced by a template rule). It's largely a matter of taste which solution you want to use.


Because the thumbnail node lying inside the object node, the need for a separated mode has gone. Basically, the whole template becomes easier and more straightforward. I upgraded it to work with your input. A side effect of the chosen xpaths is that it also works with a structure that has more than one thumbnail inside an object node. I added some comments for clarification.

Consider placing "5" (your mod value) in a global variable. That will ease modification later on. Putting it in a parameter will make it configurable from the processor (if you can set parameters, that is).

HtH,

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

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


<!--
This one removes default template
output from other nodes that contain text.
Replace this with the rest of
your own templates
-->
<xsl:template match="/insightResponse/searchResponse
/collectionResultSet/*" priority="0"/>
<xsl:template match="collectionResultSet">
<html><body>
<table>
<!--
you probably want to add logic here for
collection name and institution etc.
-->
<xsl:apply-templates select="object" />
</table>
</body></html>
</xsl:template>
<!-- match each fifth object node -->
<xsl:template match="object[position() mod 5 = 1]">
<xsl:variable name="pos" select="position()" />
<tr>
<xsl:apply-templates
select="thumbnail |
(following-sibling::object/thumbnail)
[position() &lt; 5]"
/>
</tr>
</xsl:template>
<!-- create a thumbnail -->
<xsl:template match="thumbnail">
<td>
<img src="{@URL}" />
<xsl:apply-templates />
</td>
</xsl:template>
<!-- create label -->
<xsl:template match="label">
<br /><xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>


Current Thread