Solved

Increment variable in XSL withing a for-each loop

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

22 Experts available now in Live!

Get 1:1 Help Now