Solved

How to exit xsl:for-each loop ?

Posted on 2002-03-07
4
4,570 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
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
wbradney 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now