?
Solved

Increment variable in XSL withing a for-each loop

Posted on 2004-08-11
5
Medium Priority
?
2,660 Views
Last Modified: 2012-06-27
Hello,

I have been trying to increment a variable within the for loop, but with little success.
I have provided the code snippet.

Currently I have the following:
id="<xsl:value-of select="generate-id()"/>"

I need to change this to
id = "<xsl:value-of select="$index + 1"/>"  or something to that effect

where the value of id gets incremented within the for loop


XSL Code Snippet
---------------------
<xsl:template match="DynamicASCXObject">
       <xsl:if test="not(preceding::DynamicASCXObject)">
             &lt; Form runat="server" &gt;
       </xsl:if>      
       <xsl:call-template name="DynamicASCXObjectContent"/><br/>
 </xsl:template>
 

 <xsl:template name="DynamicASCXObjectContent">

       <xsl:for-each select=".">
       <xsl:choose>
             <xsl:when test="not(@src = preceding::DynamicASCXObject/@src)">
                   &lt;p&gt;<br/>
                         &lt;%@ Register TagPrefix="<xsl:value-of select="@tagprefix"/>"
                                     TagName="<xsl:value-of select="@tagname"/>"
                                     Src="<xsl:value-of select="@src"/>"%&gt;<br/>
                         &lt;<xsl:value-of select="@tagprefix"/>:<xsl:value-of select="@tagname"/>
                                     id="<xsl:value-of select="generate-id()"/>"
                                     runat="server"
                                     parameters="<xsl:value-of select="@parameters"/>"/&gt; <br/>
                   &lt;/p&gt;<br/><br/>
             </xsl:when>
             <xsl:otherwise>
                   &lt;p&gt;<br/>
                         &lt;<xsl:value-of select="@tagprefix"/>:<xsl:value-of select="@tagname"/>
                                     id="<xsl:value-of select="generate-id()"/>"
                                     runat="server"
                                     parameters="<xsl:value-of select="@parameters"/>"/&gt; <br/>
                   &lt;/p&gt;<br/><br/>
             </xsl:otherwise>
       </xsl:choose>
       </xsl:for-each>
 </xsl:template>


Any help on this will be greatly appreciated.
Thanks.
0
Comment
Question by:lavan_8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:DominicCronin
ID: 11777601
The problem is that variables just don't work that way in XSLT. Once you have given a value to a variable, that value remains for ever. This is weird if you are used to procedural programming languages, but not so weird for people coming from the background of functional programming languages like Lisp. You can probably achieve what you want to do by using recursion.

In other words, you need to write this so that in one branch of your <xsl:choose> you do <xsl:call-template name="DynamicASCXObjectContent"> .
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 1000 total points
ID: 11778179
Well, the short answer is you can't.  You can do it recursively, as in:

http://rdcpro.com/Members/rdcpro/snippets/recursionandcounting/

But that's overkill here, I think.  I can think of two ways to accomplish your goal without recursion:

<xsl:value-of select="position()"/>

which only works if the current node's position is what you want...which is often not the case.  Or you can be a little more discerning, like:

<xsl:value-of select="count(preceding::DynamicASCXObject)"/>

or some similar XPath expression that points to the correct nodelist.

Also, I'm assuming your xsl:output is set to method="text", right?

Regards,
Mike Sharp
0
 
LVL 1

Author Comment

by:lavan_8
ID: 11778329
The output is set to the following:

<xsl:output
      method="xml"
      omit-xml-declaration ="yes"
      indent="yes"
/>

I did try using position and <xsl:value-of select="count(preceding::DynamicASCXObject)"/> but I don't get an incremental value...

 
0
 
LVL 1

Author Comment

by:lavan_8
ID: 11785262
I used the following to generate a number in my case
<xsl:value-of select="count(preceding::node())

Thank you for that great tip Mike!
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11785701
Glad that worked for you.

Doesn't this:

<xsl:output
     method="xml"
     omit-xml-declaration ="yes"
     indent="yes"
/>

cause problems for you when the <@ Register stuff and the other ASP.NET tags get escaped?

If you run into problems, change the output method to "text" and the escaping will go away.  

Regards,
Mike Sharp
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question