This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes xsl looping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xsl looping" Watch "xsl looping" New topic
Author

xsl looping

Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254

This might be a newbie question .
Is there any tag called xsl:while for looping .
I am trying to loop in xsl based on a tag value .
Say if the tag value is 10 . It will loop 10 times .
Something like below.
<xsl:variable name="count">
<xsl:value-of select="COUNT"/>
</xsl:variable>
<xsl:while localtest="{$count} < 4">
Hello!!<BR/>
<xsl:variable name="count" value="{$count} + 1"/>
</xsl:while>
am i doing something wrong??
if xsl:while is invalid tag how can i do this
thanks
kripal

# Help an unprivileged kid.<br /> Whatever u do will make a difference...<br /> ...to a child's life & ur own #<br /><a href="http://www.cry.org/" target="_blank" rel="nofollow">www.cry.org/</a>
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
You hit the main point of XSLT programming! XSLT belongs to a "functional programming" paradigm. In functional languages there is no iteration, only function calls. If you need to perform some actions n times, you achieve this by wrapping these actions as a function (template in our case) and calling it recursively.
But there is a trick which makes this job easier (M.Kay �) For example, if you want to output "Hello!!<BR/>" $count times, you do this:
<xsl:for-each select="//node()[position() &lt;= $count]">
Hello!!<BR/>
</xsl:for-each>
it will work as long as there are at least $count elements in your input XML document.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
ravi bask
Ranch Hand

Joined: Jul 05, 2001
Posts: 58
hi kripal,
U can do that, i guess u can find a iterations code(xslt) in bet, i don't remember the site, but it helps u a lot,
u hv xsl:while ,xsl:for , all iterations tag,
u can also do this in by writing a java code and using the java code in xslt programming,
check out this to in net,
hv fun
rav
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
I would be surprised if there is not a looping tag for xslt. Iteration is basically what the BodyTagSupport in JSP tag extension was based on(which is XML BTW).
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
I would be surprised if there is not a looping tag for xslt.
There is not. All standard XSLT elements are well-known , neither xsl:while, nor xsl:for exist. There is such thing as "element extensions", proprietary for XSLT processors, but core standard XSLT doesn't provide iteration statements. This is not unique XSLT's feature, Lisp, another functional language, doesn't have them either.
Iteration is basically what the BodyTagSupport in JSP tag extension was based on(which is XML BTW).
JSP is essentially Java statements, wrapped as XML elements - it has nothing to do with XSLT. XSLT is a subset of XML, which can be executed by specially designed for this purpose software - XSLT processors. JSP tags are executed by Servlet engine, if I am not mistaken, but definitely not by XSLT processors.
check out this to in net
I did and the only relevant message was this:
<xsl:while> doesn't make sense unless variables can be updated, which they can't: your <xsl:variable> element is illegal. Saxon does have a <saxon:assign> and <saxon:while> pair of instructions, but it's a good idea to learn to use XSLT "properly" before you resort to them.
Kay Michael http://www.biglist.com/lists/xsl-list/archives/200005/msg01024.html

[This message has been edited by Mapraputa Is (edited August 04, 2001).]
ravi bask
Ranch Hand

Joined: Jul 05, 2001
Posts: 58
hey guys ,
i know u guys are wondering about xsl:while xsl:for,
i know, i like to tell u something,
when i was working in project (anthem blue cross blue shield) in cincinnati, i had a similiar problem, i searched thru the net for looping one ,atlast i found one ,but i don't remember the site,
i worked with it ,it worked fine, i will try to get u guys an example,
Todays world nothing is impossible,
u know what that code is in production now, i heard it's still running,,
san
ravi bask
Ranch Hand

Joined: Jul 05, 2001
Posts: 58
i forgot one thing, it's third party tool
ravi bask
Ranch Hand

Joined: Jul 05, 2001
Posts: 58
i guess this should help u guys,
<xsl:variable name="start" select="number(1)"/>
<xsl:variable name="increment" select="number(1)"/>
<xsl:variable name="end" select="number(10)" />
<xsl:call-template name="loop">
<xsl:with-param name="counter" select="$start"/>
</xsl:call-template>
<xsl:template name="loop">
<xsl aram name="counter"/>
<xsl:if test="$counter <= $end">
your logic
<xsl:call-template name="loop">
<xsl:with-param name="counter" select="$counter + $increment"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

a tricky way to do that
ravi bask
Ranch Hand

Joined: Jul 05, 2001
Posts: 58
<xsl:call-template name="looper">
<xsl:with-param name="iterations" select="'5'"/>
</xsl:call-template>
<xsl:template name="looper">
<xsl aram name="iterations"/>
<xsl:if test="$iterations > 0">
<!-- This is the body of your for loop-->
<!-- do something here -->
<!-- recursive call -->
<xsl:call-template name="looper">
<xsl:with-param name="iterations" select="$iterations - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

here u go this one works , i tried it
Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
thanks ravi
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Here is a good article about loop emulation in XSLT:
Getting Loopy, by Bob DuCharme

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: xsl looping
 
Similar Threads
need to generate some numbers using loop, the number of times condition met
need to generate some numbers using loop, the number of times condition met
Replicate XML Node using XSL
Need help with "Variable"
XSLT loop