Repeating data based on varying items in an array

Friday, 13 May 2022 · 12 messages · last reply 16 May 2022
ME
Markus Elfring13 May 2022 12:46 #
Hello, It seems that I am still not familiar enough with the programming language “XQuery” for the quick specification of a bit of code for a general data processing task. Thus I hope that the following use case can be clarified better. A sequence like “(["f" [1 2 3]], ["g" [4, 5]])” might be an interesting test example. It contains two nested arrays. * One part refers to fixed data. * Another part refers to some items with a varying number. I would like to convert the varying items into a data representation like the following. f,1 f,2 f,3 g,4 g,5 Can any customised functions help for the desired transformation? Regards, Markus
MH
Martin Honnen13 May 2022 13:07 #
Quoted text · Markus Elfring · 5 lines
Am 13.05.2022 um 12:46 schrieb Markus Elfring: > > > A sequence like “(["f" [1 2 3]], ["g" [4, 5]])” might be an interesting test example. > It contains two nested arrays.
That is not even syntactically close to a sequence of two nested arrays, I am afraid.
AB
Andy Buncein reply to Markus Elfring, 13 May 2022, 12:4613 May 2022 13:38 #
Hi Markus, Possibly the below is helpful to you /Andy
declare function local:foo($item as array(*)){
  let $f:=$item(1)
  let $v:=$item(2)
  return array:for-each($v, function($q){ [$f,$q] }) ?*
};
(["f", [1, 2, 3]], ["g", [4, 5]]) ! local:foo(.)
Quoted text · Markus Elfring · 34 lines
On Fri, 13 May 2022 at 11:47, Markus Elfring <Markus.Elfring at web.de> wrote: > Hello, > > It seems that I am still not familiar enough with the programming language > “XQuery” > for the quick specification of a bit of code for a general data processing > task. > Thus I hope that the following use case can be clarified better. > > > A sequence like “(["f" [1 2 3]], ["g" [4, 5]])” might be an interesting > test example. > It contains two nested arrays. > > * One part refers to fixed data. > > * Another part refers to some items with a varying number. > > > I would like to convert the varying items into a data representation like > the following. > > f,1 > f,2 > f,3 > g,4 > g,5 > > > Can any customised functions help for the desired transformation? > > Regards, > Markus >
ME
Markus Elfringin reply to Martin Honnen, 13 May 2022, 13:0713 May 2022 13:47 #
Quoted text · Martin Honnen · 2 lines
> That is not even syntactically close to a sequence of two nested arrays, > I am afraid.
How do you think about to extend an XQuery sccript variant like the following for the desired transformation?
declare option output:method "csv";
declare option output:csv "header=yes";
for $x in (["f", [1, 2, 3]], ["g", [4, 5]])
return
<csv>
<record>
<key>{$x(1)}</key>
<values>{$x(2)}</values>
</record>
</csv>
Test result: key,values f,1 2 3 g,4 5 Regards, Markus
ME
Markus Elfringin reply to Andy Bunce, 13 May 2022, 13:3813 May 2022 14:00 #
Quoted text · Andy Bunce · 1 line
> Possibly the below is helpful to you
Thanks for your suggestion. Regards, Markus
MH
Martin Honnenin reply to Markus Elfring, 13 May 2022, 13:4713 May 2022 14:04 #
Quoted text · Markus Elfring · 23 lines
Am 13.05.2022 um 13:47 schrieb Markus Elfring: > > How do you think about to extend an XQuery sccript variant like the following > for the desired transformation? > > declare option output:method "csv"; > declare option output:csv "header=yes"; > for $x in (["f", [1, 2, 3]], ["g", [4, 5]]) > return > <csv> > <record> > <key>{$x(1)}</key> > <values>{$x(2)}</values> > </record> > </csv> > > > Test result: > key,values > f,1 2 3 > g,4 5 > >
Your result asked for in the original question, if it also was meant to indicate some CSV representation, could be achieved using
declare option output:method "csv";
declare option output:csv "header=yes";

<csv>
{
(["f", [1, 2, 3]], ["g", [4, 5]]) !
(let $key := ?1, $values := ?2?* return $values!
<record>
<key>{$key}</key>
<value>{.}</value>
</record>
)
}
</csv>
ME
Markus Elfring13 May 2022 15:08 #
Quoted text · Martin Honnen · 18 lines
> Your result asked for in the original question, if it also was meant to > indicate some CSV representation, could be achieved using > > > declare option output:method "csv"; > declare option output:csv "header=yes"; > > <csv> > { > (["f", [1, 2, 3]], ["g", [4, 5]]) ! > (let $key := ?1, $values := ?2?* return $values! > <record> > <key>{$key}</key> > <value>{.}</value> > </record> > ) > } > </csv>
Can such an XQuery script variant be adapted also for the handling of items from a subtree instead of the shown number array? Regards, Markus
MH
Martin Honnen13 May 2022 15:26 #
Quoted text · Markus Elfring · 4 lines
Am 13.05.2022 um 15:08 schrieb Markus Elfring: > Can such an XQuery script variant be adapted also for the handling of items > from a subtree instead of the shown number array? >
Can you show us the input structure? A sequence of arrays with a string literal as the first array item but an element or document node as the second array item? I don't see any problem using e.g.
$values := ?2//foo/data() return $values !
instead of
$values := ?2?* return $values !
ME
Markus Elfring16 May 2022 10:18 #
Quoted text · Martin Honnen · 3 lines
> Can you show us the input structure? A sequence of arrays with a string > literal as the first array item but an element or document node as the > second array item?
I stumbled on the design challenge if the sequence of nested arrays would be constructed by another FLWOR expression (instead of the mentioned test data literal). Would such an XQuery script variant become more interesting? Are any more information sources available for similar use cases? Regards, Markus
MH
Martin Honnen16 May 2022 11:47 #
Quoted text · Markus Elfring · 6 lines
Am 16.05.2022 um 10:18 schrieb Markus Elfring: >> Can you show us the input structure? A sequence of arrays with a string >> literal as the first array item but an element or document node as the >> second array item? > I stumbled on the design challenge if the sequence of nested arrays would be > constructed by another FLWOR expression (instead of the mentioned test data literal).
To me that kind of array with a string literal followed by a an array of integers seemed kind of an odd data structure to start with so I would think about just adapting the original FLOWR expression to perhaps directly output XML or a sequences of maps from string to an array of integers. But I don't see any difference in XQuery in general or for that sample you had, it doesn't really matter whether you have a literal or construct the data from another FLOWR expression. Did you run into any problems/errors using the data from a FLOWR expression? If you need help with that perhaps show a sample and the error you got.
ME
Markus Elfringin reply to Andy Bunce, 13 May 2022, 13:3816 May 2022 11:55 #
Quoted text · Andy Bunce · 14 lines
> declare function local:foo($item as array(*)){ > let $f:=$item(1) > let $v:=$item(2) > return array:for-each($v, function($q){ [$f,$q] }) ?* > }; > > (["f", [1, 2, 3]], ["g", [4, 5]]) ! local:foo(.) > declare function local:foo($item as array(*)){ > let $f:=$item(1) > let $v:=$item(2) > return array:for-each($v, function($q){ [$f,$q] }) ?* > }; > > (["f", [1, 2, 3]], ["g", [4, 5]]) ! local:foo(.)
I am still looking for further clarification according to affected implementation details. * I would appreciate also if the declaration of such callback functions can be described better in published software documentation for the programming language “XQuery”. * How would you convert the constructed array to a CSV file format? * How should similar for-each operations be performed for other data structures (besides arrays)? * Would you like to compare run time characteristics any more for such data processing? Regards, Markus
ME
Markus Elfringin reply to Martin Honnen, 16 May 2022, 11:4716 May 2022 14:32 #
Quoted text · Martin Honnen · 2 lines
> To me that kind of array with a string literal followed by a an array of > integers seemed kind of an odd data structure to start with
I am curious how such views will evolve further.
Quoted text · Martin Honnen · 1 line
> so I would think about just adapting the original FLOWR expression
I am trying this for a while.
Quoted text · Martin Honnen · 2 lines
> to perhaps directly output XML or a sequences of maps from string > to an array of integers.
I would prefer other data structures for the discussed use case at the moment. I would like to achieve that output data can be used for the construction of N:M relationships within a database. https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#Cardinalities
Quoted text · Martin Honnen · 3 lines
> But I don't see any difference in XQuery in general or for that sample > you had, it doesn't really matter whether you have a literal or > construct the data from another FLOWR expression.
I got the impression that the clarification of implementation details would get more challenging the more expressions will be involved finally.
Quoted text · Martin Honnen · 1 line
> Did you run into any problems/errors using the data from a FLOWR expression?
The answer depends on corresponding views. I am struggling also with the XQuery processor in the way that there are further issues to consider until this development tool will agree to my data processing intentions according to evolving script variants for some use cases. I would appreciate a bit more guidance for some error messages.
Quoted text · Martin Honnen · 1 line
> If you need help with that perhaps show a sample and the error you got.
Further test examples might follow for advanced expression combinations. Regards, Markus

⚡Generated with XQuery