Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xquery- how to increment a counter variable within a for loop/ how to convert array of string values into node

Tags:

xquery

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?

like image 537
Arvind Avatar asked Oct 07 '22 15:10

Arvind


1 Answers

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.

like image 178
Leo Wörteler Avatar answered Oct 10 '22 01:10

Leo Wörteler