Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 02:27 - Dec 19, 2006  

dustPuppy

How to make a for loop in xslt (not for-each)?

I need to call a template 5 times, so I need to make something like
for (int i = 1; i <=5; i++)
{
xxx
}

How is that accomplished in xslt?

Categories

Answer Discussion
Tutorials

 

dustPuppy

Date:: Dec 19, 2006

Time:: 02:50

I found this way, that should work supposedly:

<xsl:for-each select="//*[position() &lt;= 5]">

</xsl:for-each>

But it only does the first step of the loop.

dustPuppy

Date:: Dec 19, 2006

Time:: 03:30

It seems this is not the "right" way to use XSLT after all:

"incrementing a counter
is a procedural programming solution, while XSLT is a functional language.
Sometimes you do have to resort to using recursion or an extension, but often
there is a way to achieve the results you want without resorting to using a
counter at all, let alone incrementing it. The most common problems are solved
using careful selection of nodes using the select attribute on an xsl:for-each
or xsl:apply-templates, and utilizing position() when processing those nodes."
Quoted from: http://www.xslt.com/html/xsl-list/2002-0...

dustPuppy

Date:: Dec 19, 2006

Time:: 03:35

This is the best way to do it (recursively):

<xsl:template name="for.loop">
<xsl:param name="i" />
<xsl:param name="count" />
<!--begin_: Line_by_Line_Output -->
<xsl:if test="$i &lt;= $count">
<br /> <b><xsl:value-of select="$i" />.</b>Hello world!
</xsl:if>

<!--begin_: RepeatTheLoopUntilFinished-->
<xsl:if test="$i &lt;= $count">
<xsl:call-template name="for.loop">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="count">
<xsl:value-of select="$count"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>

</xsl:template>

<xsl:call-template name="for.loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="count">10</xsl:with-param>
</xsl:call-template>

xarcus

Date:: Dec 19, 2006

Time:: 10:23

XSL is based on a "transformation" model
The best way to follow the model is to make appear an element 5 times on the source xml

dustPuppy

Date:: Feb 10, 2007

Time:: 06:04

gotta close this one...
Xarcus, good comment there :)

mrousseva

Date:: Jan 31, 2012

Time:: 18:25

use similar to this:

<xsl:for-each select="1 to xs:integer($srcNode/cellData[nodeId = $nodeId]/level - 1)">

where $srcNode/cellData[nodeId = $nodeId]/level - 1) returnes some valid integer

Question Answered

This question has been closed, and points have been rewarded to the following experts:


dustPuppy: 65
xarcus: 10

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es