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 November 19th, 2007, 07:33 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt:for-each - how can I exit/terminate a loop?

I use <xsl:for-each.. and <xsl:if.. tags to query for a matching element's attribute (e.g. "name='TIN").

If exists Then
  I would like to EXIT the "xsl:for-each" loop immediately.

Note: if I do not exit it immediately, my post-action of the querying will repeat my preferred post-action multiple times.

How can I exit/terminate a loop? Can it be done using an alternative approach?

Please help! Happy Thanksgiving 07.

 
Old November 19th, 2007, 07:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You have to treat xsl:for-each as if all the items in the input are processed in parallel rather than sequentially. If you think of it that way, it's immediately obvious that "exiting the loop" isn't a concept that makes sense.

There are several ways you can process all the items in a sequence up to the first one that satisfies some condition. The most elegant way is recursion: write a template or function that processes the first in the sequence and then calls itself to process the rest of the sequence, exiting when the condition is satisfied. If the items are sibling nodes in document order, you can find the first one F that satisfies the condition, and then do xsl:for-each select="$seq[. << $F]" (in 2.0) or $F/preceding-sibling::* (in 1.0).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 19th, 2007, 08:28 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

After reading your quick response of a new recursion to process a list of values. I immediately recognized a good example in page 224 of your book "XSLT 2.0 third edition" I bought recently.

I have tried it out. It works. Thank so much.




Quote:
quote:Originally posted by mhkay
 You have to treat xsl:for-each as if all the items in the input are processed in parallel rather than sequentially. If you think of it that way, it's immediately obvious that "exiting the loop" isn't a concept that makes sense.

There are several ways you can process all the items in a sequence up to the first one that satisfies some condition. The most elegant way is recursion: write a template or function that processes the first in the sequence and then calls itself to process the rest of the sequence, exiting when the condition is satisfied. If the items are sibling nodes in document order, you can find the first one F that satisfies the condition, and then do xsl:for-each select="$seq[. << $F]" (in 2.0) or $F/preceding-sibling::* (in 1.0).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exit from loop dishant XSLT 2 October 6th, 2008 03:34 AM
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 06:04 PM
Application.exit() fails to terminate application. saumya General .NET 1 January 4th, 2008 09:50 AM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 02:27 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 04:46 PM





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