473,491 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XSLT: number of times template called

i'm trying to do the classic alternating table row colors trick.
i can't use position() mod 2 in this case because sometimes the
template is called but it decides not to print the particular element.
i really need to keep track of how many times it has printed something,
but since you can't modify variables in xslt, i have no idea how to
do it.

the fragment looks something like this:

<xsl:template match="story">
<xsl:param name="no_repeats"/>
<xsl:if test="following-sibling::*[1]/id != id or position() = last() or $no_repeats != 1">
<tr>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">dark</xsl:when>
<xsl:otherwise>light</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<td>

any ideas?
-rs-
Jul 20 '05 #1
1 1384
sn***@nospam.com (Ralph Snart) writes:
i'm trying to do the classic alternating table row colors trick.
i can't use position() mod 2 in this case because sometimes the
template is called but it decides not to print the particular element.
i really need to keep track of how many times it has printed something,
but since you can't modify variables in xslt, i have no idea how to
do it.
Even if you could modify variables in xslt it wouldn't work, as a
processor is not obliged to execute the templates in natural order, it
can execute them in any order it wants (and in particular it can execute
them in parallel) If it can spot that two of the input nodes are
equivalent and the matching template doesn't use context information
like position() it need not evaluate the template multiple times at all,
it can just copy the result. So the value of a variable would be
completely undefined in that case as it depends on internal processor
behaviour.
the fragment looks something like this:

<xsl:template match="story">
<xsl:param name="no_repeats"/>
<xsl:if test="following-sibling::*[1]/id != id or position() = last() or $no_repeats != 1">
<tr>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">dark</xsl:when>
<xsl:otherwise>light</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<td>

any ideas?
-rs-


The easiest is probably to change that to

<xsl:template match="story">
<xsl:param name="no_repeats"/>
<tr>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">dark</xsl:when>
<xsl:otherwise>light</xsl:otherwise>
</xsl:choose>
</xsl:attribute>

and change
<xsl:apply-templates select="story"/> in the template matching the
parent to

<xsl:apply-templates select="story[following-sibling::*[1]/id != id or position() = last() or $no_repeats != 1]"/>

So that you only select the elements you want so position() counts the
elements you want rather than selecting everything then throwing some
away.

David
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2679
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
3
1622
by: Mike Whittemore | last post by:
I am trying to convert an HTML table into a list of name-value pairs, one pair per field in the table. I believe my XSLT is correct, but I've tried both Xalan and Saxon, which both fail with...
3
7344
by: Son KwonNam | last post by:
The title is quite ambiguos. Any way let me explain. In java, the following is possible. ---- String str = ""; for (int i=0; i < 10; i++) { str = str + i; } ----
4
1817
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
1
2709
by: Nick | last post by:
I am working on a website for a client and one of their requirements was to have a mailing list. I decided to XSLT to transform "templates" to HTML so that editing was very easy and less time...
0
3280
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
4
1909
by: shaun roe | last post by:
I should like to count the frequency of strings embedded in a longer string, space separated. Specifically, I have: <phiModule> 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 8 5 5 5 6 6 6 7 7 7 7 7 7 7 7...
2
22746
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
1
3436
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.