File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xsl looping

 
Kripal Singh
Ranch Hand
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ravi bask
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i forgot one thing, it's third party tool
 
ravi bask
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ravi
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good article about loop emulation in XSLT:
Getting Loopy, by Bob DuCharme

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic