HELP! For structure in XSLT

Joined
May 13, 2004
Messages
4
Reaction score
0
Hi, I need to make a FOR structure using XSL... ie. I want to make a structure for moving between 0 and x, being x a variable name... How can I do this? In PHP/Java would be something like this:

for($i=0;$i<LIMIT;$i++)
{
// Something using $i
}
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Miguel said:
I need to make a FOR structure using XSL... ie. I want to make a structure for moving between 0 and x, being x a variable name... How can I do this? In PHP/Java would be something like this:

for($i=0;$i<LIMIT;$i++)
{
// Something using $i
}

Well, XSLT has <xsl:for-each> but in XSLT 1.0 you can't use that to build a simple loop through some sequence so you have to write a template and call that recursively to implement such a loop.
 

Saqib Ali

Usenet Archive
Joined
Oct 9, 2002
Messages
74
Reaction score
0
Hi, I need to make a FOR structure using XSL... ie. I want to make a
structure for moving between 0 and x, being x a variable name... How can I do this? In PHP/Java would be something like this:

for($i=0;$i<LIMIT;$i++)
{
// Something using $i
}

Try the <xsl:for-each> tag

In Peace,
Saqib Ali
http://validate.sf.net
 

Andy Dingley

Usenet Archive
Joined
Feb 23, 2000
Messages
3,303
Reaction score
0
Miguel J. Jiménez said:
How can I do this?

Recursion. Make a function that calls itself, counts how far down it has gone (pass a paramater and increment it each time), then returns when it is deep enough.

Alternatively, don't do it - there's usually a more "XSL like" alternative that either doesn't require the loop, or that replaces it with iteration over a set (using <xsl:for-each ... >)

If you're desperate, you can embed some data as a list in your stylesheet, then use <xsl:for-each select="document('')/foo:data/*"
to iterate over that. Be warned though, this is an extreme technique
that's only appropriate in _very_ rare cases. Most of the time you might think you "need" it, but that would give you bad XSLT style.

Overall, I suggest you read a copy of Michael Kay's XSLT book, or Mangano's "XSLT Cookbook"
 

Robin Johnson

Usenet Archive
Joined
Oct 26, 2000
Messages
25
Reaction score
0
Miguel J. Jiménez said:
Hi, I need to make a FOR structure using XSL... ie. I want to make a structure for moving between 0 and x, being x a variable name... How can I do this?

The short answer is, you don't. But here's a template that uses recursion to do pretty much the same:

<xsl:template name="recur">
<xsl:param name="n" select="number(1)"/>
<!-- 1-based rather than 0-based is more XML-ian :) --> <xsl:param name name="max"/>

<!-- your code here. $n holds the current loop number if you want to refer to it.

Note that changing this template to iterate downwards is a little more efficient and correct, IMO. Then you have no $max, you specify $n as your highest value, and the test is $n &gt; 0 and encases this whole section rather than just the recursive call-template.
-->

<xsl:if test="$n &lt; $max">
<xsl:call-template name="recur">
<xsl:with-param name="max" select="$max"/>
<xsl:with-param name="n" select="1 + $n"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

Call that template specifying $max as the value you want to iterate up to. If you must start at 0 rather than 1, specify $n as 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top