title: How to pass page information to form using XSL variables with properly defined scope?
body: <p>I have a loop in my XSL template that loops through each "event" and displays certain information. Each event has a form associated with it. </p>
  
  <p>I'd like to store some of the event information in variables so that I can pass it along with the form submission.</p>
  
  <p>First, I tried to give the variable a simple name (e.g. "event_id"):</p>
  
  <pre><code>&lt;xsl:for-each select="event"&gt;
      &lt;div class="event"&gt;
          &lt;div class="event_id"&gt;Event ID: &lt;xsl:value-of select="@id" /&gt;&lt;/div&gt;
          &lt;xsl:variable name="event_id"&gt;&lt;xsl:value-of select="@id" /&gt;&lt;/xsl:variable&gt;
      &lt;/div&gt;
      &lt;form action="submit_label" method="post"&gt;
          &lt;input type="hidden" name="event_id" value="{$event_id}"&gt;&lt;/input&gt;
      &lt;/form&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>But I get this error when the page gets to the form:</p>
  
  <blockquote>
    <p>Could not compile stylesheet:
    file: .../war/jstl:: line 81: Variable or parameter 'event_id' is undefined.</p>
  </blockquote>
  
  <p>Pastebin XSL code: <a href="http://pastebin.com/CUgqxptb" rel="nofollow noreferrer">http://pastebin.com/CUgqxptb</a><br>
  Pastebin error message: <a href="http://pastebin.com/EuW0aV71" rel="nofollow noreferrer">http://pastebin.com/EuW0aV71</a></p>
  
  <p>I'm assuming this is because the variable and form are in a for loop, and names need to be unique. (Is that right?! -- UPDATE: this is not right). Although, confusingly, this does not produce the same error:</p>
  
  <pre><code>&lt;form&gt;
      &lt;xsl:variable name="foo"&gt;bar&lt;/xsl:variable&gt;
      &lt;input type="text" name="{$foo}" placeholder="{$foo}"&gt;&lt;/input&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>So, what I'm trying now is this, with the aim that I can reference the event variable in the form input field using a counter (i):</p>
  
  <pre><code>&lt;xsl:variable name="i" value="0"&gt;&lt;/xsl:variable&gt; 
  &lt;xsl:for-each select="event"&gt;
      &lt;div class="event_id"&gt;Event ID: &lt;xsl:value-of select="@id" /&gt;&lt;/div&gt;
      &lt;xsl:variable name="event_{$i}"&gt;&lt;xsl:value-of select="@id" /&gt;&lt;/xsl:variable&gt;
  
      &lt;form action="submit_label" method="post"&gt;
          &lt;input type="hidden" name="event_id" value="{$event_i}"&gt;&lt;/input&gt;
      &lt;/form&gt;
  
      $i++
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>(I know <code>value="{$event_i}"</code> doesn't make any sense, but hopefully you get the idea)</p>
  
  <p>This gives me the following error about <code>&lt;xsl:variable name="event_{$i}"&gt;</code>: </p>
  
  <blockquote>
    <p>An attribute whose value must be a QName or whitespace-separated list of QNames had the value 'event_{$i}'. </p>
  </blockquote>
  
  <p>I tried using <code>&lt;xsl:variable name=&lt;fn:resolve-QName("event",$i) /&gt;&gt;</code>, but then I get this error:</p>
  
  <blockquote>
    <p>Open quote is expected for attribute associated with an  element type  "name".</p>
  </blockquote>
  
  <p>But, of course, adding quotes (<code>&lt;xsl:variable name="&lt;fn:resolve-QName('event',$i) /&gt;"&gt;</code>), gives me this error:</p>
  
  <blockquote>
    <p>The value of attribute "name" associated with an element type "null" must not contain the '&lt;' character.</p>
  </blockquote>
  
  <p>So, any suggestions about how to get a hold of the event_id information in order to send it along with my form? Thanks!!!</p>
  
  <p><strong>UPDATE:</strong><br>
  Progress! Thanks to @Lukas comment below (referencing <a href="https://stackoverflow.com/a/9261566/1590763">https://stackoverflow.com/a/9261566/1590763</a>), I'm now trying to declare the variables outside of the for-loop. This is working OK, but I think I'll need to do the variable business 5 different times for EACH event-level variable, which seems pretty bloated... Example here: <a href="http://pastebin.com/EuW0aV71" rel="nofollow noreferrer">http://pastebin.com/EuW0aV71</a></p>
  
  <p><strong>UPDATE #2:</strong><br>
  Don't need to do the variable business 5 times. Just needed to move some div tags around. Thanks to @TimC for talking me through it. Final working version of the code:</p>
  
  <pre><code>&lt;xsl:for-each select="event"&gt;
      &lt;xsl:variable name="event_id"&gt;&lt;xsl:value-of select="@id" /&gt;&lt;/xsl:variable&gt;        
      &lt;div class="event"&gt;
          &lt;div class="event_id"&gt;Event ID: &lt;xsl:value-of select="@id" /&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;form action="submit_label" method="post"&gt;
          &lt;input type="hidden" name="event_id" value="{$event_id}"&gt;&lt;/input&gt;
      &lt;/form&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>Full code here: <a href="http://pastebin.com/gP06gYEg" rel="nofollow noreferrer">http://pastebin.com/gP06gYEg</a></p>