Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7875383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:58:25+00:00 2026-06-03T02:58:25+00:00

How do I exit a for-each loop in XSL if a condition is satisfied?

  • 0

How do I exit a “for-each” loop in XSL if a condition is satisfied?
e.g. Suppose I want the XSL to display the address of apartments which have (2 bedrooms and rent <= 1000), in the following XML, the following XSL code:

<xsl:for-each select="//apartment/apartment_details">
  <xsl:if test="bedrooms=$bedrooms and rent &lt;= $budget "> 
    <!--display apartment address--> 
  </xsl:if>
</xsl:for-each>

would return the same apartment address twice. I want to display the apartment address only once even if there are multiple for the apartment that satisfy the condition.

XML structure:

<apartments>
  <apartment>
    <address>
        <street>....</street>
        <city>....</city>
    </address>
    <apartment_details>
        <bedrooms>2</bedrooms>
        <bathrooms>2</bathrooms>
        <rent>1000</rent>
    </apartment_details>
    <apartment_details>
        <bedrooms>2</bedrooms>
        <bathrooms>1</bathrooms>
        <rent>900</rent>
    </apartment_details>
    ...
  </apartment>
  ...
</apartments>

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:58:25+00:00Added an answer on June 3, 2026 at 2:58 am

    How do I exit a “for-each” loop in XSL if a condition is satisfied?

    This is not possible. There isn’t any XSLT instruction for exiting the processing of xsl:for-each and continuing the execution of the transformation. What you can do is specify precisely the conditions that the selected nodes should meet.

    Use:

    <xsl:for-each select=
     "/*/apartment
          [apartment_details[bedrooms=$bedrooms and $budget  >= rent]]">
      <!-- output apartment address here -->  
    </xsl:for-each>
    

    This code displays the address of any apartment that is a child of the top element of the XML document and that has an apartment_details child, for whose children bedrooms and rent it is true() that: bedrooms=$bedrooms and $budget >= rent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I exit from $.each loop when a condition is met? I don't
I have setup a simple while loop which returns all images in a table
I have a master shell script which called a child script for each iteration
In my application I have a loop that in each turn creates an instance
I have two SQL scripts which get called within a loop that accept a
vwait forever runs the events loop until the exit command. I have some stuff
I have a GUI program with one Exit button. When clicking this button, it
I'd like the aspect to exit a method invocation based on a condition like
i have a list of images which i am getting through ajax and then
I dont understand why, when i run my code the for each loop under

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.