Solved

Increment variable in XSL withing a for-each loop

Posted on 2004-08-11
5
2,650 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
  • 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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, split 12 80
Which free XML software can I use for opening and editing an XML file? 15 110
XML Parsing Classic ASP 5 56
Configure a Bean in an XML file 4 31
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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. 
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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