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 at
keyword inside the for
clause:
let $var := tokenize('Mohan Prakash, Ramesh Saini', ', ')
for $t at $pos in $var
return concat($t, ',', $pos)
This returns the two strings Mohan Prakash,1
and Ramesh Saini,2
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With