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
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
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>
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
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 !
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
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.
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.
so I would think about just adapting the original FLOWR expression
I am trying this for a while.
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#Cardinalitie...
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.
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.
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
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(.)
On Fri, 13 May 2022 at 11:47, Markus Elfring Markus.Elfring@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
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
basex-talk@mailman.uni-konstanz.de