Solved

How to exit xsl:for-each loop ?

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT Display Label Name 1 42
Selenium and Xpath 4 69
Health check of winows and webservces 5 52
XML Constructor Throwing Incorrectly Structured Document Error 4 24
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 …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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