472,328 Members | 1,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 26206
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
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 ...
2
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...
19
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...
6
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
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...
11
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?...
2
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...
3
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...
2
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 =...
4
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

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.