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.
I'd like to store some of the event information in variables so that I can pass it along with the form submission.
First, I tried to give the variable a simple name (e.g. "event_id"):
<xsl:for-each select="event">
<div class="event">
<div class="event_id">Event ID: <xsl:value-of select="@id" /></div>
<xsl:variable name="event_id"><xsl:value-of select="@id" /></xsl:variable>
</div>
<form action="submit_label" method="post">
<input type="hidden" name="event_id" value="{$event_id}"></input>
</form>
</xsl:for-each>
But I get this error when the page gets to the form:
Could not compile stylesheet: file: .../war/jstl:: line 81: Variable or parameter 'event_id' is undefined.
Pastebin XSL code: http://pastebin.com/CUgqxptb
Pastebin error message: http://pastebin.com/EuW0aV71
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:
<form>
<xsl:variable name="foo">bar</xsl:variable>
<input type="text" name="{$foo}" placeholder="{$foo}"></input>
</form>
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):
<xsl:variable name="i" value="0"></xsl:variable>
<xsl:for-each select="event">
<div class="event_id">Event ID: <xsl:value-of select="@id" /></div>
<xsl:variable name="event_{$i}"><xsl:value-of select="@id" /></xsl:variable>
<form action="submit_label" method="post">
<input type="hidden" name="event_id" value="{$event_i}"></input>
</form>
$i++
</xsl:for-each>
(I know value="{$event_i}"
doesn't make any sense, but hopefully you get the idea)
This gives me the following error about <xsl:variable name="event_{$i}">
:
An attribute whose value must be a QName or whitespace-separated list of QNames had the value 'event_{$i}'.
I tried using <xsl:variable name=<fn:resolve-QName("event",$i) />>
, but then I get this error:
Open quote is expected for attribute associated with an element type "name".
But, of course, adding quotes (<xsl:variable name="<fn:resolve-QName('event',$i) />">
), gives me this error:
The value of attribute "name" associated with an element type "null" must not contain the '<' character.
So, any suggestions about how to get a hold of the event_id information in order to send it along with my form? Thanks!!!
UPDATE:
Progress! Thanks to @Lukas comment below (referencing https://stackoverflow.com/a/9261566/1590763), 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: http://pastebin.com/EuW0aV71
UPDATE #2:
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:
<xsl:for-each select="event">
<xsl:variable name="event_id"><xsl:value-of select="@id" /></xsl:variable>
<div class="event">
<div class="event_id">Event ID: <xsl:value-of select="@id" /></div>
</div>
<form action="submit_label" method="post">
<input type="hidden" name="event_id" value="{$event_id}"></input>
</form>
</xsl:for-each>
Full code here: http://pastebin.com/gP06gYEg