Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 3rd, 2007, 04:14 PM
Registered User
Join Date: Jul 2007
Location: Miami, FL, .
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.

Reply With Quote
  #2 (permalink)  
Old July 4th, 2007, 01:19 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old July 4th, 2007, 02:18 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 08:05 PM.

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