title: Need help finding first non-empty element in XML using XSLT
body: <p>XML element contains both empty and non-empty values. I need to capture the first non-empty element. I have tried for-each loops with a conditional when/choose or if statements but reading other posts, it seems this should be simple to do using select="" in XSL.</p>
  
  <p>For-each with choose/if conditional statements always returns multiple values when I only want the first non-empty element and its value to be 
  returned/assigned to my variable or directly within XSL HTML code.</p>
  
  <p>I have also tried following this example for doing something similar:
  <a href="https://stackoverflow.com/questions/16031673/get-the-non-empty-element-using-xpath">Get the non-empty element using XPATH</a></p>
  
  <p>but my variable always returns 'empty'.</p>
  
  <p>Here is a snippet of the XML I am processing with both empty and non-empty element values for ifid:</p>
  
  <pre><code>&lt;transaction&gt;
  
  &lt;SurePayDetails&gt;
  &lt;Subfield&gt;
  
  &lt;ifid&gt;&lt;/ifid&gt;
  
  &lt;/Subfield&gt;
  &lt;/SurePayDetails&gt;
  
  &lt;SurePayDetails&gt;
  &lt;Subfield&gt;
  
  &lt;ifid&gt;&lt;/ifid&gt;
  
  &lt;/Subfield&gt;
  &lt;/SurePayDetails&gt;
  &lt;SurePayDetails&gt;
  &lt;Subfield&gt;
  
  &lt;ifid&gt;0022&lt;/ifid&gt;
  &lt;/Subfield&gt;
  &lt;/SurePayDetails&gt;
  
  &lt;/transaction&gt;
  </code></pre>
  
  <p>I have tried accessing/assigning to a variable the first non-empty
   element using the following:</p>
  
  <p><code>&lt;xsl:variable name="auth_code" select="/transaction/SurePayDetails/Subfield/ifid[text()[string-length(.) &gt; 0]][1]"/&gt;</code></p>
  
  <p>which as I understand it will select the first ifid element whose length is greater than 0 and <code>[1]</code> selects that value.</p>
  
  <p>I am not sure if I am referencing the XML correctly in the above <code>select=""</code> because if I traverse the document with the following:</p>
  
  <pre><code>`&lt;xsl:for-each select="//ns1:transaction/ns1:SurePayDetails/ns1:Subfield"&gt;
  .
  .
  &lt;xsl:variable name="auth_code" select="ns1:ifid"/&gt;`
  </code></pre>
  
  <p>the auth_code is populated with both empty and non-empty values as expected.</p>
  
  <p>Thank you!</p>