Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Incrementing a variable value in XSL and storing in hidden control

Posted on 2003-03-26
7
Medium Priority
?
773 Views
Last Modified: 2012-05-04
Dear Experts,

I have a requirement wherein a variable has to be incremented based on a condition and store the result in a hidden control.  To put in simple VB flow...

Dim x as integer
x=0
While(condition is true)
   If (1 < 2) then
      x=x+1
   End if
Wend

And finally,
<Store the value of X in a hidden control>

Can I have the equivalent code in XSL please?

<xsl:variable name="x">0</xsl:variable>
<xsl:for loop>
    <xsl:if condition>
       <Increment the value here>
    </xsl:if>
</xsl:for>
<!-- storing in a hidden control-->
<input type="hidden" name="xyz">
  <xsl:attribute name="value"><xsl:value-of select="$VariableName"/></xsl:attribute>
</input>

Well, I am aware that variable value, once assigned cannot be modified in XSL.  But, I vaguely know that it can achieved using parameters.  Please help me to solve this.

Any help on this would be greatly appreciated.

Thanks in advance but points will be awarded after solving the problem.

Cheers,
0
Comment
Question by:haibala
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:avner
ID: 8211000
No.
In XSL you cannot change a variable.
For the part of the incrementation you will have to change the value of a variable, this cannot be done in XSL.

0
 

Author Comment

by:haibala
ID: 8215308
I agree, its not possible with variables in XSL. Is there any other way?  Can we do it with parameters?  If yes, I would like to know the syntax.

Thanks.
0
 
LVL 14

Expert Comment

by:avner
ID: 8215747
Try to look at this approach, the allows you to loop a certain template for few times, You you can set the start-val and end-val dynamically :


Try this approach :
<!-- put this in an existing template -->
  <xsl:call-template name="loop">
 <xsl:with-param name="start-val" select="'1'"/>
 <xsl:with-param name="end-val" select="'10'"/>
</xsl:call-template>


<!-- loops from $start-val to $end-val with a step value of
  $increment
-->
<xsl:template name="loop">
<xsl:param name="start-val" select="'0'"/>
<xsl:param name="end-val" select="'0'"/>
<xsl:param name="increment" select="'1'"/>


<xsl:if test="$start-val <= $end-val">

       <!-- do stuff here, i just print out the number -->
       VALUE:    <xsl:value-of select="$start-val"/>

 <!-- recurse -->
 <xsl:call-template name="loop">
   <xsl:with-param name="start-val" select="$start-val + $increment"/>
   <xsl:with-param name="increment" select="$increment"/>
   <xsl:with-param name="end-val" select="$end-val"/>
 </xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


0
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!

 
LVL 1

Expert Comment

by:xsl-lsx
ID: 8218677
There is way keeps it simple:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <msxsl:script language="VBScript"
    implements-prefix="user"><![CDATA[

     numb=0
         
     Function Count()
          numb = numb + 1
          Count = numb
     End Function

  ]]></msxsl:script>


 <xsl:template match="input[whatever]">
    <xsl:for-each select="whatever">
     <xsl:if test="@hidden">
        <input type="hidden" name="xyz" value="{user:Count()}"/>
     </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

0
 
LVL 1

Accepted Solution

by:
xsl-lsx earned 1500 total points
ID: 8218823
The following does it without param thus simple:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:user="http://yourname.com/yourtask"

xmlns:msxsl="urn:schemas-microsoft-com:xslt">

 <msxsl:script language="VBScript"
   implements-prefix="user"><![CDATA[

    numb=0
         
    Function Count()
         numb = numb + 1
         Count = numb
    End Function

 ]]></msxsl:script>


<xsl:template match="input[whatever]">
   <xsl:for-each select="whatever">
    <xsl:if test="@hidden">
       <input type="hidden" name="xyz" value="{user:Count()}"/>
    </xsl:if>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>
0
 
LVL 1

Expert Comment

by:xsl-lsx
ID: 8219398
XSL has immutable variables, which means the variable must receive their perpetual value on its creation and there after never be changed.  The param is a variable, on the stack. That is, they must be given a value on the call but never change during the call.  Thus, recursion of routines allows the programmer to pass a new value (i.e. incremented) to the next call.  Recursion is very useful for many problems with trees, HOWEVER, not universally applicable to many procedural problems as you have requested in using a for-loop.   Perhaps your problem can become recursive thus applicable.
0
 

Author Comment

by:haibala
ID: 11971959
Thanks very much for all the inputs
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

580 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