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

  • SEARCH
  • Home
  • 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 988583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:39:27+00:00 2026-05-16T05:39:27+00:00

I need to change the order in which my for-each is executed only if

  • 0

I need to change the order in which my for-each is executed only if some conditions are met.

Here is what my XML looks like :

<OptionList>
    <Option name="My First Option" />
    <Option name="My Second Option" />
</OptionList>

However, in some case, my XML can be like this :

<OptionList>
    <Option />
    <Option name="My Second Option" />
</OptionList>

In my XSL, I’m doing a for-each like this :

<xsl:for-each select="//OptionList/Option">
    {...}
</xsl:for-each>

I know I can change orders of Option nodes using this line in my for-each :

<xsl:sort select="position()" data-type="number" order="descending" />

The problem is that I want my order to be descending only when my first Option node is empty and doesn’t have the name attribute. Otherwise, I want to keep the default ascending order.

Any input on how I can acheive that? So far, everything I tried ended up with “Variable out of scope” or invalid use of xpath functions.

  • 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-05-16T05:39:28+00:00Added an answer on May 16, 2026 at 5:39 am

    You can use a hack to change the sort order based on a condition:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/">
        <result>
        <xsl:for-each select="//OptionList/Option">
          <xsl:sort data-type="number" order="ascending"
            select="position()*(-2*number(not(//OptionList/Option[1]/@name))+1)"/>
    
          <option>
            <xsl:value-of select="@name"/>
          </option>
        </xsl:for-each>
        </result>
    
      </xsl:template>
    </xsl:stylesheet>
    

    The hack is that number((true()) returns 1 and number(false()) returns 0. As a consequence, the expression

    -2 * number(not(//OptionList/Option[1]/@name)) + 1
    

    evaluates to 1 if the first option element has a name attribute and to -1 otherwise. This is used as a factor to reverse the sort order.

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

Sidebar

Related Questions

I have situation where I need to change the order of the columns/adding new
I need to change some text values inside an after effect project file that
I need to store some data in a file. FOR THE MOMENT each record
I need to implement a large collection of Widget objects, each of which contain
I need change background of all text that have two spaces from the start
i need change color of SystemTray.ProgressIndicator color, how i can do it? I tried
I have database, and in one the tables I need change the values of
I use Pjax with tutorial from http://railscasts.com/episodes/294-playing-with-pjax?view=comments I don't need change url and this
Need to change li position by clicking to move up or to move down.
I need to change RichEditor and TextEditor modes with JavaScript, now I need to

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.