XSL: break for-each loop Help required !


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: XSL: break for-each loop Help required !

  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, 12:48 PM
  2. plz help me n00b here
    By Apocalyp5e in forum Java
    Replies: 11
    Last Post: 12-22-2005, 09:03 PM
  3. Replies: 1
    Last Post: 11-23-2002, 02:40 AM
  4. Replies: 0
    Last Post: 04-06-2000, 10:32 PM

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center