title: How can I change the order of an xsl for-each?
body: <p>I need to change the order in which my for-each is executed only if some conditions are met.</p>
  
  <p>Here is what my XML looks like :</p>
  
  <pre><code>&lt;OptionList&gt;
      &lt;Option name="My First Option" /&gt;
      &lt;Option name="My Second Option" /&gt;
  &lt;/OptionList&gt;
  </code></pre>
  
  <p>However, in some case, my XML can be like this :</p>
  
  <pre><code>&lt;OptionList&gt;
      &lt;Option /&gt;
      &lt;Option name="My Second Option" /&gt;
  &lt;/OptionList&gt;
  </code></pre>
  
  <p>In my XSL, I'm doing a for-each like this :</p>
  
  <pre><code>&lt;xsl:for-each select="//OptionList/Option"&gt;
      {...}
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>I know I can change orders of Option nodes using this line in my for-each :</p>
  
  <pre><code>&lt;xsl:sort select="position()" data-type="number" order="descending" /&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>