Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > for-each and incrementing by the count() function

Reply
Thread Tools

for-each and incrementing by the count() function

 
 
Ken
Guest
Posts: n/a
 
      07-15-2003
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.
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-16-2003
This is easy and straightforward.

This transformation:

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

<xslutput 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.



 
Reply With Quote
 
 
 
 
Ken
Guest
Posts: n/a
 
      07-16-2003
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dictionaries and incrementing keys Steve Crook Python 8 06-25-2011 08:13 AM
ActiveRecord and incrementing the key Abder-Rahman Ali Ruby 5 09-06-2010 02:58 PM
incrementing string/hex value from file and write back Matthias GŁntert Python 1 08-20-2009 09:35 PM
Problems storing objects in a vector and incrementing a staticcounter in C++ Bruno.DiStefano C++ 3 09-05-2008 05:58 PM
Writing a incrementing/decrementing function with roll-over pozz C Programming 10 07-14-2006 12:49 PM



Advertisments