Solved

Increment variable in XSL withing a for-each loop

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now