473,799 Members | 2,772 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 26331
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
12695
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
1925
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
3830
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
23882
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
11528
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
1351
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
2637
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
2849
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 header information - headers already sent by (output started at...
2
1912
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
2315
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 target compilation). So far I didn't touch the topic "nested functions", since I just don't see an...
0
9689
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5469
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2942
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.