Solved

Increment variable in XSL withing a for-each loop

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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