Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exit condition in an XSL for loop - need help

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of for loops in a template to collect a string value. Once I find the value
I want to exit out of the for loops and move on.
Wondering if there is any equivalent to a "break" statement in XSL...
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no explicit "breaks" in XSLT. You can try to code your if condition in "select" clause of <xsl:for-each> element. Something like
<xsl:for-each select="./ListMember[. = $value]">
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to look at other ways of doing this, like trying to see if there is a way to tweak the position()value in the loop to last(), once i meet the required condition. I don't think that is possible. I think Mapraputa's solution also gives you the flexibility of when to exit out of the loop by adding the position() check inside the for-each loop for the list of nodes matching the $value. The other thing that came to my mind as a derivative of this is the following -
If i say
<xsl:if test="position()=1">...</xsl:if>, inside the <xsl:for-each...> loop, will it still execute the position() information for each node even after we go across the required one. I guess this is true and this may be a little redundancy we have to live with. This is something similar to if true(A & B), then if A is false, it doesn't evaluate the other one for truth, i.e., a little optimization. If we had something similar to a break of C language, that would be really good.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Map.
That works.
- madhav
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayadev, I can be wrong here, but it looks to me that you are trying to reason about XSLT in terms of a paradigm it doesn't belong to. "If we had something similar to a break of C language", then XSLT would be C. XSLT is a functional language, C or Java are not. You *can* re-state one concept (functional language) in terms of another (imperative language), and in Satya's case his variant probably maps to
for (i=1; ;i++ ) {
do something
if (i==someValue) break;
}

amd mine to
for (i=1; i != someValue ;i++ ) {
do something
}
But this is only syntactic sugar and it doesn’t provide too deep an insight into the semantics of the language.
This is from Raphael Finkel's "Advanced Programming Language Design" (let the word "advanced" scare nobody here, it isn't that much advanced )
"Some languages are imperative; they use variables, assignments, and iteration. Other languages are functional; they have no variables, assignments, or iteration, but model program execution as expression evaluation."
Back to our break, one solution, "as originally described by Jeni Tennison, is not to use an xsl:for-each construct at all, but to replace it with a (recursively applied) template":
http://www.vbxml.com/code/default.asp?p=3&id=v20010228100123
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is not to use an xsl:for-each construct at all, but to replace it with a (recursively applied) template":

I did that in quite a few of my template rules. But somehow, this time I wanted to be a little different. Guess I was getting bored with all the resurcion that I was doing. Now, for this problem I was thinking of recursing like this:

Thanks.
- madhav
ps:
I haven't tested this rule but just wrote it for practice, in case I ever have the same doubt again... ;)
[ December 07, 2002: Message edited by: Madhav Lakkapragada ]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking that there must be an easier way to do it...
Madhav, if you want to test whther there is an element with one particular value, then one XPath expression should do that.
<xsl:if test="//ListMember='3'">
found...
</xsl:if>
(I tested it on the stupidest XML I could think about :
<ListMembers>
<ListMember>1</ListMember>
<ListMember>2</ListMember>
<ListMember>3</ListMember>
<ListMember>4</ListMember>
<ListMember>5</ListMember>
</ListMembers>
)
--------------------------------
"How complicated can processing a few cuddly little tags peppered with some good old PCDATA be?"
Sean McGrath. The Real Cost of XML Tags
[ December 08, 2002: Message edited by: Mapraputa Is ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic