Solved

How to exit xsl:for-each loop ?

Posted on 2002-03-07
4
4,579 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 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…

628 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