title: How to use variables in xsl:for-each but create the output later?
body: <p>I'm struggling to get the output to display as required.</p>
  
  <p>I run an xslt 1.0 file that parses every 5 seconds a xml file that contains statistical information.
  every 15 minutes we get an additional file to parse which is pulled in as external source.
  The parsing is fine but displaying the information fails miserably.
  This is the section that creates the output:</p>
  
  <pre><code>&lt;td&gt;
    &lt;table cellSpacing="0" border="1" cellPadding="2" style="background-color:lightblue;color:green;font-size:80%;"&gt;
      &lt;xsl:for-each select="/DialStats/Countries"&gt;
        &lt;!-- @Country = 15 min 2nd XML file / $Country = 5 sec prim XML file--&gt;
        &lt;xsl:if test="@Country = $Country"&gt;
            &lt;xsl:for-each select="./Products"&gt;
              &lt;!-- @Product = 15 min 2nd XML file / $Product = 5 sec prim XML file--&gt;
              &lt;xsl:if test="@Product = $Product"&gt;
                &lt;xsl:for-each select="Dispositions"&gt;
                  &lt;xsl:variable name="TSB"&gt;
                    &lt;xsl:if test="@Result = 'Tour / Sale / Booking'"&gt;
                      &lt;xsl:value-of select="@Count"/&gt;
                     &lt;/xsl:if&gt;
                   &lt;/xsl:variable&gt;
  &lt;!-- // set multiple variables:  --&gt;
                   &lt;xsl:variable name="ITF"&gt;
                     &lt;xsl:if test="@Result = 'Internal Transfer'"&gt;
                       &lt;xsl:value-of select="@Count"/&gt;
                     &lt;/xsl:if&gt;
                   &lt;/xsl:variable&gt;
  
  &lt;!-- // Now generate the output:  --&gt;
                   &lt;xsl:choose&gt;
                     &lt;xsl:when test="string-length($TSB)=0 and string-length($NoAgent)=0"&gt;
                       &lt;!-- Don't do anything --&gt;
                     &lt;/xsl:when&gt;  
                     &lt;xsl:when test="string-length($TSB)!=0 and string-length($NoAgent)=0"&gt;
                       &lt;tr&gt;
                         &lt;td&gt;
                           &lt;xsl:value-of select="$TSB"/&gt;
                         &lt;/td&gt;
                         &lt;td&gt; - &lt;/td&gt;
                         &lt;td&gt;TSB&lt;/td&gt;
                         &lt;td&gt;. .&lt;/td&gt;
                         &lt;td colspan="3"&gt;&lt;/td&gt;
                       &lt;/tr&gt;
                     &lt;/xsl:when&gt;
                     &lt;xsl:when test="string-length($TSB)=0 and string-length($NoAgent)!=0"&gt;
                       &lt;tr&gt;
                         &lt;td colspan="3"&gt;&lt;/td&gt;
                         &lt;td&gt;. .&lt;/td&gt;
                         &lt;td&gt;
                           &lt;xsl:value-of select="$NoAgent"/&gt;
                         &lt;/td&gt;
                         &lt;td&gt;-&lt;/td&gt;
                         &lt;td&gt;No Agent&lt;/td&gt;
                       &lt;/tr&gt;
                     &lt;/xsl:when&gt;
                     &lt;xsl:otherwise&gt;
                       &lt;tr&gt;
                         &lt;td&gt;
                           &lt;xsl:value-of select="$TSB"/&gt;
                         &lt;/td&gt;
                         &lt;td&gt; - &lt;/td&gt;
                         &lt;td&gt;TSB&lt;/td&gt;
                         &lt;td&gt;. .&lt;/td&gt;
                         &lt;td&gt;
                           &lt;xsl:value-of select="$NoAgent"/&gt;
                         &lt;/td&gt;
                         &lt;td&gt;-&lt;/td&gt;
                         &lt;td&gt;No Agent&lt;/td&gt;
                       &lt;/tr&gt;
                     &lt;/xsl:otherwise&gt;
                   &lt;/xsl:choose&gt;
  &lt;!-- // Generate more of the output:  --&gt;
  &lt;!-- // Last section  --&gt;
                    &lt;xsl:choose&gt;
                      &lt;xsl:when test="string-length($OTH)=0"&gt;
                        &lt;!-- Don't do anything --&gt;
                      &lt;/xsl:when&gt;
                      &lt;xsl:when test="string-length($OTH)!=0"&gt;
                        &lt;tr&gt;
                          &lt;td&gt;
                            &lt;xsl:value-of select="$OTH"/&gt;
                          &lt;/td&gt;
                          &lt;td&gt;-&lt;/td&gt;
                          &lt;td&gt;OTH&lt;/td&gt;
                          &lt;td&gt;. .&lt;/td&gt;
                          &lt;td colspan="3"&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                      &lt;/xsl:when&gt;
                      &lt;xsl:otherwise&gt;
                        &lt;!-- Nothing to do --&gt;
                      &lt;/xsl:otherwise&gt;
                    &lt;/xsl:choose&gt;
                 &lt;/xsl:for-each&gt;
               &lt;/xsl:if&gt;
             &lt;/xsl:for-each&gt;
           &lt;/xsl:if&gt;
         &lt;/xsl:for-each&gt;
     &lt;/table&gt;
   &lt;/td&gt;
  </code></pre>
  
  <p>This generates a table however due to how the XML file contains information it is not consistently in the same spot, it might not be there at all. 
  I want the information to show on the same place for different blocks.</p>
  
  <p>This is how I want it to display</p>
  
  <pre>
    x - TSB  | x - NoAgent
    x - OSA  | x - LO
    x - IP   | x - UEE
    x - MES  | x - SIT
    x - NoVM | x - RNA
    x - NSR  | x - ITF
    x - OTH
  </pre>
  
  <p>If the value is 0 then I leave the info blank if both are blank then I want to leave the whole line out.</p>
  
  <p>This is how it however displays</p>
  
  <pre>
  9 - MES  |   
  3 - NoVM |  
  7 - IP   |  
           | 5 - LO 
  6 - OTH  |  
           | 6 - RNA 
           | 4 - SIT 
  3 -  TSB | 
  </pre>
  
  <p>Is there a way to created first all variables before creating the output?
  I tried that but then I end up in the problem that the variable is out-of-scope.</p>
  
  <p>Looking forward to suggestions</p>
  
  <p>The XML looks like this:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;DialStats&gt;
      &lt;Countries Country="France"&gt;
       &lt;Products Product="Office"&gt;
         &lt;Dispositions Result="Agent Voicemail / Message" Count="11" /&gt;
         &lt;Dispositions Result="Did Not Leave Voicemail" Count="37" /&gt;
         &lt;Dispositions Result="Information Provided" Count="13" /&gt;
         &lt;Dispositions Result="Lost Opportunity" Count="1" /&gt;
         &lt;Dispositions Result="No Agent Available" Count="1" /&gt;
         &lt;Dispositions Result="Ring No Answer" Count="6" /&gt;
         &lt;Dispositions Result="SIT Tone" Count="3" /&gt;
         &lt;Dispositions Result="Tour / Sale / Booking" Count="7" /&gt;
       &lt;/Products&gt;
     &lt;/Countries&gt;
     &lt;Countries Country="Italy"&gt;
       &lt;Products Product="Office"&gt;
         &lt;Dispositions Result="Did Not Leave Voicemail" Count="1" /&gt;
         &lt;Dispositions Result="Information Provided" Count="6" /&gt;
         &lt;Dispositions Result="Non-Sales Related" Count="1" /&gt;
         &lt;Dispositions Result="Other" Count="6" /&gt;
         &lt;Dispositions Result="Ring No Answer" Count="7" /&gt;
         &lt;Dispositions Result="SIT Tone" Count="5" /&gt;
         &lt;Dispositions Result="Updated Existing Enquiry" Count="13" /&gt;
       &lt;/Products&gt;
     &lt;/Countries&gt;
     &lt;Countries Country="Netherlands"&gt;
       &lt;Products Product="Office"&gt;
         &lt;Dispositions Result="Agent Voicemail / Message" Count="1" /&gt;
         &lt;Dispositions Result="Information Provided" Count="17" /&gt;
         &lt;Dispositions Result="Ring No Answer" Count="3" /&gt;
         &lt;Dispositions Result="Tour / Sale / Booking" Count="2" /&gt;
       &lt;/Products&gt;
    &lt;/Countries&gt;
   &lt;/DialStats&gt;
  </code></pre>
  
  <p>Many different Countries possible.</p>
  
  <p>Thanks
  <img src="https://i.stack.imgur.com/z7InA.png" alt="Output"></p>