|
I had a puzzling problem with a xforms:repeat and index() today. I copied the bookcast sample to create my own application. I changed the xforms:repeat to be:
<xforms:repeat nodeset="*:scenario" id="scenario-repeat">
I added some buttons to each item that call servlets. I kept clicking on the buttons in my list of items, and the wrong items would be affected by the button actions. For example, I'd click on the first item in the list, and the third would be affected. So I added debug info at the top of the xhtml to output the currently selected index and @name.
<p>Now editing '<xforms:output value="instance('scenarios-instance')/scenario[index('scenario-repeat')]/@name"/>' with index <xforms:output value="index('scenario-repeat')"/></p>
This displayed the wrong name when I clicked on an item (one chosen seemingly at random), but the index was right! It's like my instance is being reordered somewhere behind the scenes after it is displayed.
I added similar code to the xforms-bookcast application, and it works flawlessly.
<p> DEBUG... index is <xforms:output value="index('book-repeat')"/>, <xforms:output value="instance('books-instance')/book[index('book-repeat')]/title"/></p>
After some frustrating hours I decided to edit the XPath a little in my debug statement.
<p>Now editing '<xforms:output value="instance('scenarios-instance')/*[index('scenario-repeat')]/@name"/>' with index <xforms:output value="index('scenario-repeat')"/></p>
Note that I replaced 'scenarios' in the XPath with '*' and... drumroll... it works! When I select an item it shows the correct name in the debug output. I edited my submissions to use this new Xpath and those started working too.
It seems that using the node name (scenario) in the XPath reorders the list, whereas using * returns a list that still has the same order. Note that the nodeset for the xforms:repeat is scenarios, not *.
This work around is OK for now because I don't currently have any other type of element in the list. But I can't guarantee this in the future!
Is this a bug or actually how things should operate?
|