473,554 Members | 5,365 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="it em0_event0"/>
<event description="it em0_event1"/>
</item>
<item id="1" name="item1">
<event description="it em1_event0"/>
<event description="it em1_event1"/>
</item>

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

<xsl:for-each select="item">
<xsl:variable name="baseRow" select="positio n()"/>
<row>
<xsl:attribut e name="index"><x sl:value-of
select="$baseRo w"/></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:attribut e name="index"><x sl:value-of select="$baseRo w
+ position()"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@descri ption"/>
</cell>
</row>
</xsl:for-each>
<!-- <xsl:variable name="baseRow" select="$baseRo w +
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 26309
This is easy and straightforward .

This transformation:

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

<xsl:output omit-xml-declaration="ye s" 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="{$vRowIn d - 1}">
<xsl:for-each select="@*">
<cell index="{positio n() - 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="it em0_event0"/>
<event description="it em0_event1"/>
</item>
<item id="1" name="item1">
<event description="it em1_event0"/>
<event description="it em1_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.goo gle.com...
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="it em0_event0"/>
<event description="it em0_event1"/>
</item>
<item id="1" name="item1">
<event description="it em1_event0"/>
<event description="it em1_event1"/>
</item>

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

<xsl:for-each select="item">
<xsl:variable name="baseRow" select="positio n()"/>
<row>
<xsl:attribut e name="index"><x sl:value-of
select="$baseRo w"/></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:attribut e name="index"><x sl:value-of select="$baseRo w
+ position()"/></xsl:attribute>
<cell index="0">
<xsl:value-of select="@descri ption"/>
</cell>
</row>
</xsl:for-each>
<!-- <xsl:variable name="baseRow" select="$baseRo w +
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
12668
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
1909
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 The question may be silly or even meaning less but please............
19
3812
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
23870
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
11515
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 number)
11
1322
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
2624
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 function... I have mentioned my doubts in code comments. My code is... void myfun_callback(void(*fp)(char*)) //is this function is a call back? {
3
2833
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 /home/notasluz/public_html/administrador/Administrador.php:1) in /home/notasluz/public_html/administrador/verificaEntrada.php on line 2  Warning: Cannot modify...
2
1891
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
2293
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 could implement an equivalent behaviour with plain C (in this case I'm thinking of the language I'm developing, which shall be converted into C for...
0
7581
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7872
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6123
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5423
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5142
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2006
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 we have to send another system
1
1114
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.