HELP! For structure in XSLT

  • Thread starter =?ISO-8859-15?Q?=22Miguel_J=2E_Jim=E9nez=22?=
  • Start date
?

=?ISO-8859-15?Q?=22Miguel_J=2E_Jim=E9nez=22?=

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
}

--
Miguel J. Jiménez
ISOTROL, S.A. (Área de Internet)
Avda. Innovación nº1, 3ª - 41020 Sevilla (ESPAÑA)
mjjimenez AT isotrol DOT com --- http://www.isotrol.com
ICQ# 12670750
TLFNO. 955036800 ext. 111
 
M

Martin Honnen

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.
 
S

Saqib Ali

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
 
A

Andy Dingley

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"
 
R

Robin Johnson

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.
 
?

=?ISO-8859-1?Q?=22Miguel_J=2E_Jim=E9nez=22?=

Ok, I know THAT, but suppose I do not have any elements to use with
<xsl:for-each>... I want to make the FOR from scratch....

Saqib said:
Try the <xsl:for-each> tag

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

--
Miguel J. Jiménez
ISOTROL, S.A. (Área de Internet)
Avda. Innovación nº1, 3ª - 41020 Sevilla (ESPAÑA)
mjjimenez AT isotrol DOT com --- http://www.isotrol.com
ICQ# 12670750
TLFNO. 955036800 ext. 111
 

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
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top