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 October 6th, 2008, 01:29 AM
Authorized User
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to exit from loop

Hi All,

I have written a logic for validation of xml request node (i.e. value of node), here i am using for each loop to get value of node , but for each loop will check for vlaue of all the node present, but i want to exit from loop when any validation fails how to do that


            <xsl:for-each select="Configuration/Node">
                    <xsl:if test="$Mandatory='Y'">
                        <xsl:if test="$Data=''">
                            <xsl:variable name="Result" select="'FAIL'"/>
                            <xsl:variable name="ErrorValue" select="concat('Mandatory',$Data,',')"/>
                    <xsl:if test="$Length!=string-length($Data)">
                        <xsl:variable name="Result" select="'FAIL'"/>
                        <xsl:variable name="ErrorValue" select="concat('Result',$Data,',')"/>
                    <xsl:if test="$Content!=$Data">
                        <xsl:variable name="Result" select="'FAIL'"/>
                        <xsl:variable name="ErrorValue" select="concat('Content',$Data,',')"/>

Thanks in Advance

Old October 6th, 2008, 02:45 AM
Friend of Wrox
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts

No you cant exit a loop. Try to write the code in terms of what
you want it to do rather than how you want to do it. First you can check for $Mandatory !='Y'. Also you can use recursive templates. Show your input xml. It may help for better understanding.

Old October 6th, 2008, 03:34 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

xsl:for-each is not a loop, therefore you cannot exit from it. It's a mapping operator, which applies the instructions in the body of the for-each to each node selected by the select expression, and you should think of it processing all the nodes in parallel.

If you want the equivalent of a loop, use a recursive function or template: process the first item in the sequence, then call yourself recursively to process the next item in the sequence, terminating when your condition is true or when the end of the sequence is reached.

Your code looks very odd. The conditions you are testing depend only on variables declared outside the for-each, so if they are true for one node then they are true for every node. And the only processing you do is to bind some variables, which are inaccessible outside the for-each.

Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference

Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt:for-each - how can I exit/terminate a loop? john7321 XSLT 2 November 19th, 2007 08:28 PM
on exit mohiddin52 Access VBA 2 July 10th, 2007 09:28 AM
exit from for-each preethi.s XSLT 1 September 8th, 2006 05:37 AM
Application.Exit() Help rodmcleay C# 2 October 8th, 2003 07:54 PM
session_destroy on exit?? harmen_wessels PHP How-To 2 July 5th, 2003 09:41 PM

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