Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 3rd, 2007, 04:14 PM
Registered User
Join Date: Jul 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default break for-each loops, or limit amount of loops

How can I set a max number of loops.. Say I have 20 nodes that a select returns, but I only wan to loop for 5 of them?

in regular code:
for(i = 0; i < MYNUM; i++)

How can I accomplish this with XSLT?

Another question, how can I break out of a for-each of some CONDITION is met?

Thanks All.

Old July 4th, 2007, 01:19 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Try looking at the other questions, "breaking out of a loop" has been asked many times. Here is the answer: You can't break out of a loop as there is no such thing as a loop in XSLT. There are at least two ways to do what you need however. One is to test the position() of the node within an xsl:for-each or an xsl:template. If the position() function returns 5 or less process node else ignore it. A second way is to only select the first five nodes to start with:
<xsl:apply-templates select="myNodes[position() &lt; 6]"/>

Joe (Microsoft MVP - XML)
Old July 4th, 2007, 02:18 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

You can't break out of a for-each loop because it isn't a loop. You have to imagine that all the items in the input sequence are processed in parallel, so processing one item can't influence how or whether subsequent items are processed.

Just select the items you want to be processed in the select expression for example

select="(//node)[position() &lt;= 20]

to process the first 20 elements named "node".

If you really do need the processing of one item to depend on the result of processing previous items, you need to use recursion rather than for-each. There's the best part of a chapter in my book on this.

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops deontae45 VB.NET 2002/2003 Basics 2 September 28th, 2006 03:48 PM
Two Loops iloveoatmeal Classic ASP Basics 5 September 26th, 2005 09:59 AM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Loops Hudson40 Excel VBA 2 January 20th, 2005 07:40 AM
loops flyn Javascript How-To 1 November 21st, 2003 04:59 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.