DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2005
    Posts
    2

    XSL: break for-each loop Help required !

    Hi,

    I'm a newbee to XSLT. 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 &gt; 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 occurance. 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 occurance 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 hinderance for a big xml ?

    Any pointers will be highly appreciated.

    Thanks

  2. #2
    Join Date
    Jun 2006
    Posts
    1
    If you extract the nodes you want right off the bat in the for-each loop you should get the results you want. Just change your XPath to read: /catalog/cd[price > 10 and position() = 1]. See below.

    Code:
    <xsl:for-each select="catalog/cd[price &gt; 10 and position() = 1]">
    <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
    If that doesn't work you could also do this, although performance isn't as good:

    Code:
    <xsl:for-each select="catalog/cd[price &gt; 10]">
    <xsl:if test="position() = 1">
    <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:if>
    </xsl:for-each>

Similar Threads

  1. For Loop vs. While Loop
    By javatier in forum Java
    Replies: 16
    Last Post: 02-18-2011, 11:48 AM
  2. plz help me n00b here
    By Apocalyp5e in forum Java
    Replies: 11
    Last Post: 12-22-2005, 08:03 PM
  3. Replies: 1
    Last Post: 11-23-2002, 01:40 AM
  4. Replies: 0
    Last Post: 04-06-2000, 09:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links