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 June 10th, 2006, 08:03 PM
Registered User
 
Join Date: Jun 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default help: how can I break from xsl for-each loop ?

Hi,

  Just wanted to know if its possible to break from a for-each loop. Pls refer to the following sample : For e.g. I've a xml like this.

<catalog>
  <cd>
     <title>Empire Burlesque</title>
     <artist>Bob Dylan</artist>
     <price>10</price>
  </cd>
  <cd>
     <title>Hide your heart</title>
     <artist>Bonnie Tyler</artist>
     <price>20</price>
  </cd>
  <cd>
     <title>Greatest Hits</title>
     <artist>Dolly Parton</artist>
     <price>30</price>
  </cd>
  <cd>
     <title>Billborad Hits</title>
     <artist>John Denver</artist>
     <price>50</price>
  </cd>
</catalog>

I've the following xsl,

<xsl:for-each select="catalog/cd">
      <xsl:if test="price > 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
</xsl:for-each>

This will iterate for cd element even if the price match happens at the very first occurence. I want to stop the iteration once the match happens. In my case,the xml data I'm working on holds big amount of data. My if condition matched a primary key which will only have a single occurence in the entire xml. So all I need is
<cd>
     <title>Empire Burlesque</title>
     <artist>Bob Dylan</artist>
     <price>10</price>
  </cd>

Is it possible to avoid the unnecessary iterations,which might prove a performance issue for a big xml ?

Any help will be much appreciated.

Thanks

 
Old June 11th, 2006, 02:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't break out of a for-each loop for the simple reason that it isn't a loop. XSLT is not a procedural language.
Try to imagine that when you do a for-each all elements that are selected are processed simultaneously, in parallel so as to speak.
You can accomplish what you want by narrowing down the selection to start with:
Code:
<xsl:for-each select="catalog/cd[price &gt; 10][1]">
Remeber you asked for price greater than 10 so "Empire Burlesque" won't appear as its price is not greater than 10.


--

Joe (Microsoft MVP - XML)
 
Old June 11th, 2006, 07:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can't break out of a for-each loop, because it is not actually a loop: it is a mapping expression, which processes each item in the input sequence separately, in an arbitrary order, perhaps even in parallel. If you want to process a sequence and stop when a certain condition is reached, you need to use a recursive template that steps through the sequence one item at a time.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 31st, 2012, 11:55 AM
Registered User
 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default What If

What if there is more that has a price > 10?
 
Old May 31st, 2012, 11:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, I don't know what you want to do in that case. I was only trying to guess your requirements from the patchy statement you made and the code you supplied which we know doesn't do what you want.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 31st, 2012, 12:03 PM
Registered User
 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recursive

I'm confident doing a recursive template will work, I just haven't been able to figure it out yet. Thank you.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a HashMap in XSL? sunilnk XSLT 6 April 10th, 2017 04:09 AM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
xsl:for-each loop Tomi XSLT 3 September 7th, 2006 05:35 AM
Loop in XSL NEO1976 XSLT 31 September 5th, 2006 03:39 AM
break a for loop lxu XSLT 5 January 16th, 2004 10:38 AM





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