Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to exit xsl:for-each loop ?

Posted on 2002-03-07
4
Medium Priority
?
4,582 Views
Last Modified: 2008-03-10
Is there a way to exit a for-each loop once a particular node has been found?

Or maybe there is an approach that will give me the node I want in the first place without looping at at?

e.g., I want to retrieve the first car node with make=Honda.

<cars>
<car>
<make>Ford</make>
</car>
<car>
<make>Honda</make>
</car>
0
Comment
Question by:craigdawson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Wayne Bradney earned 200 total points
ID: 6848159
craig,

There are indeed several ways to avoid a for-loop, including but not limited to the following couple:

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
               version = "1.0" >

    <xsl:template match="/cars">
        <xsl:if test="car[make='Honda']">
            Got the Honda!
            <xsl:copy-of select="car[make='Honda']"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

It really depends on the structure of you stylesheet and what you expect to do with the node you've found (or failed to find, as the case may be).

Regards,
WMB
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6848924
if you want the first car node with the make honda, yes

the full XPath expression would be
/cars/car[make[. = 'Honda']][1]

thus, to get the node concerned
<xsl:apply-templates select="/cars/car[make[. = 'Honda']][1]" />

if you are looking for distinct values as may be the case,

<cars>
<car>
<make>Ford</make>
<model>1234</model>
</car>
<car>
<make>Honda</make>
<model>1345</model>
</car>
<car>
<make>Ford</make>
<model>4567</model>
</car>
</cars>

then you can use keys
=====================
<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="car" match="car" use="make" />
<xsl:template match="/">
<div>
<div>Car Makes</div>
<xsl:apply-templates select="//car[count(. | key('car',make)[1]) = 1]" mode="makes">
<xsl:sort select="make" />
</xsl:apply-templates>
</div>
</xsl:template>

<xsl:template match="car" mode="makes">
<div>
 <xsl:value-of select="make" />
</div>
</xsl:template>
</xsl:stylesheet>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6848973
often, asking for code to break a loop indicates improper coding and potentially, bad performance. You have to get out of the traditional programming mould in order to code XSLT properly as it is not a procedural language and there is no side-effects (as long as you don't use those naughty extensions).
0
 

Author Comment

by:craigdawson
ID: 6850313
I used the first suggestion.  Second answer also good.  Thanks all.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question