Thursday 20 March 2014

OSB 11g For-each: is index variable an integer?

In OSB you can add a for-each construct to loop over a set of elements:
OSB For-each
 
Today I encountered something strange with the index variable of the for-each. At one stage I wanted to select an element based on the index-variable, in the screendump above it is 'functieIndex'.
The xpath expression I created was something like:
$body/maat:requestDoc/maat:dataElement/maat:functies/maat:functie[$functieIndex]/text()
But although I checked the value of the variable functieIndex using alerts, I always got the complete set of 'functie'-elements. But when I explicitly filled in '1' as a index I got the denoted element, like this:
$body/maat:requestDoc/maat:dataElement/maat:functies/maat:functie[1]/text()
I checked with a co-worker, and he confirmed that what I did was just how it should work. Now, thinking my brains almost to cooking temperature, I came up with the idea: 'what if it does not know it is a number?'. So I explicitly casted the $functieIndex variable to int:
$body/maat:requestDoc/maat:dataElement/maat:functies/maat:functie[xs:int($functieIndex)]/text()
And this works! Apparently somehow OSB does not know that the variable it declares for the sake of indexing a for-each is an integer by nature.

By the way, in this example I get the value also by the for-each variable. But sometimes You need the index-variable to index from another element-set.