Solved

How to exit xsl:for-each loop ?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DOCTYPE in pubmed 5 68
character entities and entity references in XML 17 122
json format text only 4 82
Online tools to transform xml to excel using xsl 1 42
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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. 
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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