473,505 Members | 14,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for-each and incrementing by the count() function

Ken
The fact that you can not reassign a variable in XSL is an endless
source of frustration, causing you to jump through all sorts of
non-intuitive hoops.

In this case, however, the lack of reassignment has me totally
perplexed on how to achieve a solution to the following problem:

(These are illustrative fragments)

XML:

<item id="0" name="item0">
<event description="item0_event0"/>
<event description="item0_event1"/>
</item>
<item id="1" name="item1">
<event description="item1_event0"/>
<event description="item1_event1"/>
</item>

XSL: (Incorrect, this is what I'm trying to figure out)

<xsl:for-each select="item">
<xsl:variable name="baseRow" select="position()"/>
<row>
<xsl:attribute name="index"><xsl:value-of
select="$baseRow"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@id"/>
</cell>
<cell index="1">
<xsl:value-of select="@name"/>
</cell>
</row>
<xsl:for-each select="event">
<row>
<xsl:attribute name="index"><xsl:value-of select="$baseRow
+ position()"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@description"/>
</cell>
</row>
</xsl:for-each>
<!-- <xsl:variable name="baseRow" select="$baseRow +
count(event)"/> --> <!-- Pseudocode -->
</xsl:for-each>

DESIRED OUTPUT AFTER XFORM:

<row index="0">
<cell index="0">0</cell>
<cell index="1">item0</cell>
</row>
<row index="1">
<cell index="0">item0_event0"</cell>
</row>
<row index="2">
<cell index="0">item0_event1"</cell>
</row>
<row index="3">
<cell index="0">1</cell>
<cell index="1">item1</cell>
</row>
<row index="4">
<cell index="0">item1_event0"</cell>
</row>
<row index="5">
<cell index="0">item1_event1"</cell>
</row>

Notice how the row index is continuously incremented. This is my
desired output, however I cannot figure out how to accomplish it.
Essentially I like to increment the base position() of the first
for-each by the count() of the second for-each at the end of the first
loop.

Thanks for any insight you can provide.
Jul 20 '05 #1
2 26301
This is easy and straightforward.

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="item | event">
<xsl:variable name="vRowInd">
<xsl:number count="item | event" level="any"/>
</xsl:variable>
<row index="{$vRowInd - 1}">
<xsl:for-each select="@*">
<cell index="{position() - 1}">
<xsl:value-of select="."/>
</cell>
</xsl:for-each>
</row>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
when applied on your source.xml:

<items>
<item id="0" name="item0">
<event description="item0_event0"/>
<event description="item0_event1"/>
</item>
<item id="1" name="item1">
<event description="item1_event0"/>
<event description="item1_event1"/>
</item>
</items>

produces the wanted result:

<row index="0">
<cell index="0">0</cell>
<cell index="1">item0</cell>
</row>
<row index="1">
<cell index="0">item0_event0</cell>
</row>
<row index="2">
<cell index="0">item0_event1</cell>
</row>
<row index="3">
<cell index="0">1</cell>
<cell index="1">item1</cell>
</row>
<row index="4">
<cell index="0">item1_event0</cell>
</row>
<row index="5">
<cell index="0">item1_event1</cell>
</row>
Hope this helped.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"Ken" <ke***********@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
The fact that you can not reassign a variable in XSL is an endless
source of frustration, causing you to jump through all sorts of
non-intuitive hoops.

In this case, however, the lack of reassignment has me totally
perplexed on how to achieve a solution to the following problem:

(These are illustrative fragments)

XML:

<item id="0" name="item0">
<event description="item0_event0"/>
<event description="item0_event1"/>
</item>
<item id="1" name="item1">
<event description="item1_event0"/>
<event description="item1_event1"/>
</item>

XSL: (Incorrect, this is what I'm trying to figure out)

<xsl:for-each select="item">
<xsl:variable name="baseRow" select="position()"/>
<row>
<xsl:attribute name="index"><xsl:value-of
select="$baseRow"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@id"/>
</cell>
<cell index="1">
<xsl:value-of select="@name"/>
</cell>
</row>
<xsl:for-each select="event">
<row>
<xsl:attribute name="index"><xsl:value-of select="$baseRow
+ position()"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@description"/>
</cell>
</row>
</xsl:for-each>
<!-- <xsl:variable name="baseRow" select="$baseRow +
count(event)"/> --> <!-- Pseudocode -->
</xsl:for-each>

DESIRED OUTPUT AFTER XFORM:

<row index="0">
<cell index="0">0</cell>
<cell index="1">item0</cell>
</row>
<row index="1">
<cell index="0">item0_event0"</cell>
</row>
<row index="2">
<cell index="0">item0_event1"</cell>
</row>
<row index="3">
<cell index="0">1</cell>
<cell index="1">item1</cell>
</row>
<row index="4">
<cell index="0">item1_event0"</cell>
</row>
<row index="5">
<cell index="0">item1_event1"</cell>
</row>

Notice how the row index is continuously incremented. This is my
desired output, however I cannot figure out how to accomplish it.
Essentially I like to increment the base position() of the first
for-each by the count() of the second for-each at the end of the first
loop.

Thanks for any insight you can provide.

Jul 20 '05 #2
Ken
Dimitre,

Thanks for taking the time to help. The union operator is something
that I'd seen before, but I wasn't able to piece that together with
xsl:number to solve my problem.

XSL (and other functional languages) are a different species of
language than Java/C++, and as I learn XSL, I have to remember to set
aside my previous ways of thinking about solving problems.
Jul 20 '05 #3

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

Similar topics

2
12664
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
2
1900
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
19
3802
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
6
23868
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
Hi all, Let's say I have the following code: typedef struct { void (*function)(); } TYPE; void function()
6
11512
kiss07
by: kiss07 | last post by:
Dear debas and friends, 1)what is the use of NOCOPY parameter? 2)function overloading ------------------------------- which are the correct function overloading: 1) function f1(c in...
11
1320
by: noddy | last post by:
I have come across this code in a js file. It uses the function constructor in a way I have never seen. Can someone explain what is happening here? Thanks (function(){ ...lines of code... } )...
2
2612
by: Pradeep | last post by:
Hi all, Can any one explain me what is callback function.... I have written some code after reading some tutorials from internet... But I am not sure is it a right way to write a call back...
3
2824
by: mrosado | last post by:
Hi everybody, I have this problem.- The browser launch this two errors: Warning: session_start() : Cannot send session cache limiter - headers already sent (output started at...
2
1888
by: Calm_Pear | last post by:
Hi all, I have created an object with a default function; the default function exposes a public function as well. myobject = function(){
4
2286
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
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...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5028
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
4699
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
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.