In a java program, I am using Saxon Library to parse some XQuery commands.
Now, first I use tokenize() to split some text into a number of text values.
An example of the text is–
Mohan Prakash, Ramesh Saini
I use tokenize() on above text with ‘,’ as the delimiter. And store the result of tokenize in variable $var
After this, I want to loop over those text values, and give as output the following–
Mohan Prakash,1
Ramesh Saini,2
As you can see from above, the last value in each row is a counter- for first row, this value is 1, for second row this value=2 and so on…
I thought of using something like the code below–
for $t in $var/position()
return concat($var[$t], ',', $t)
However I am getting an error that I cannot use position() on $var because $var is expected to be a node.
So there are 2 ways to resolve this–
(a) Convert the values in $var to a node
I tried text{$var} but that is not giving accurate results.
How do I convert the array of values into nodes?
(b) Use the following–
for $t in $var
Here, how do I create and use a counter within the for loop, so that the count value can be output along with each value of $t?
You can use the
atkeyword inside theforclause:This returns the two strings
Mohan Prakash,1andRamesh Saini,2.