Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2009, 04:24 PM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default The keywork to come out of the loop abruptly

hi,

If I want to come out of the <xsl:for-each ...> loop or any template on some condition, how can i come out of that?

The keywork "break" in Java helps to achieve this.

Any one can help me on this.
 
Old February 23rd, 2009, 05:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Whereas a "for" expression in Java is a loop in which the items must be processed sequentially, a for-each instruction in XSLT is a mapping expression in which the items in the input can be processed in any order or in parallel. It's not a loop, so it doesnt' make sense to break out of it. If you need to process items until some condition is true, use a recursive template or function.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 2nd, 2009, 03:54 PM
Registered User
 
Join Date: Sep 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question

hi,
Thanks for the reply.

I have used the recursive loop. But still i am not able to come out when i like to.
PFB the code snippet.

<?xml version="1.0"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="one">
<xsl:with-param name="st" select="0"/>
<xsl:with-param name="end" select="5"/> </xsl:call-template>
</xsl:template>

<xsl:template name="one">
<xsl:param name="st"/>
<xsl:param name="end"/> <xsl:if test="$st = 3 ">
<xsl:text>Inside... Exit now</xsl:text> </xsl:if>
<xsl:if test="$st &lt; $end">
<xsl:call-template name="one">
<xsl:with-param name="st" select="$st + 1"/>
<xsl:with-param name="end" select="$end"/> </xsl:call-template>
</xsl:if>
</xsl:template>
<
/xsl:stylesheet>

The templae "one" will be recursively called until the condition $st = 3 is satisfied. Once the condition is satisfied, I have to come out of it. How to do that. I tried declaring some variable and assigned some junk value. But as this is declared inside the "if" block, its not visible outside.
Expecting the reply
 
Old March 2nd, 2009, 05:04 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm not sure why you're passing an end parameter and testing for $st = 3 but you could always use an xsl:choose construct:
Code:
<xsl:template name="one">
 <xsl:param name="st"/>
 <xsl:param name="end"/>
 <xsl:choose>
   <xsl:when test="$st &gt; $end ">
    <xsl:text>Exit now</xsl:text>
   </xsl:when>
   <xsl:otherwise>
      $st: <xsl:value-of select="$st" />,      <xsl:call-template name="one">
       <xsl:with-param name="st" select="$st + 1"/>
         <xsl:with-param name="end" select="$end"/>
     </xsl:call-template>
   </xsl:otherwise>
 </xsl:choose>
</xsl:template>
This will run until $st is greater than $end.
__________________
Joe
http://joe.fawcett.name/

Last edited by joefawcett; March 2nd, 2009 at 05:06 PM.. Reason: Test used < instead of >
 
Old March 2nd, 2009, 05:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As Joe's code shows, you don't have to do anything to exit. Just do nothing. I.e.

Code:
if (not finished) {
  make recursive call
}
and the exit is implicit
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
"DO" loop enolregnar VB How-To 8 June 8th, 2007 12:54 AM
Insert statement is failing abruptly puneetmittal1974 SQL Server 2000 2 February 22nd, 2006 10:54 PM
For Loop aadz5 C# 2 December 26th, 2003 07:38 AM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM
loop with out do monstermash Classic ASP Basics 3 July 15th, 2003 12:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.