Nice how simple matrix transposition can be done with JSONiq.
I just took the simple sample from Wikipedia:
http://en.wikipedia.org/wiki/TransposeThis JSONiq script just does the job:
(: let $m := pxp:json($raw); :)
declare function local:transpose($m as array()) as array()
{
[ for $i in 1 to jn:size($m(1)) return [ $m()($i) ] ]
};
let $m := [
[1,2,3],
[4,5,6]
]
return local:transpose($m)
It generates this output:
[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
Unfortunately just now "Share" does not work on
http://try.zorba.io but here is Xidel permalink for playing:
http://videlibri.sourceforge.net/cgi-bin/xidelcgi?&data=[%20[1%2C2%2C3]%2C%20[4%2C5%2C6]%20]&=&extract=%28%3A%20%20let%20%24m%20%3A%3D%20pxp%3Ajson%28%24raw%29%3B%20%20%3A%29%0A%0Adeclare%20function%20local%3Atranspose%28%24m%20as%20array%28%29%29%20as%20array%28%29%0A{%0A%20%20[%20%20for%20%24i%20in%201%20to%20jn%3Asize%28%24m%281%29%29%20%20return%20[%20%24m%28%29%28%24i%29%20]%20%20]%0A}%3B%0A%0Alet%20%24m%20%3A%3D%20[%20%0A%20%20[1%2C2%2C3]%2C%20%0A%20%20[4%2C5%2C6]%20%0A]%0A%0Areturn%20local%3Atranspose%28%24m%29%0A&=&input-format=auto&printed-node-format=text&output-format=adhoc&compatibility=Standard%20XQuery%2BJSONiq&dot-notation=off&extract-kind=xquery1&no-extended-strings=true&only-json-objects=true&strict-type-checking=true&strict-namespaces=true&case-sensitive=trueDataPower has implemented version 0.4.42 of JSONiq Extension to XQuery which does not provide Array unboxing:
http://www.jsoniq.org/docs/JSONiqExtensionToXQuery/html-single/index.html#idm62098192Good that the spec itself gives the implementation, and so DataPower can use "local:unbox()" function.
This is JSONiq script transpose.xq:
declare namespace output = "
http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "json";
declare option jsoniq-version "0.4.42";
declare function local:unbox($a as array()) as json-item()*
{
for $i in 1 to jn:size($a) return $a($i)
};
declare function local:transpose($m as array()) as array()
{
[ for $i in 1 to jn:size($m(1)) return [ local:unbox($m)($i) ] ]
};
local:transpose(.)
Recently I did publish coproc2all service which extends coproc2 to XQuery and JSONiq processing for DataPower:
https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014590913#f5915ec6-99ff-426d-a3d2-c71d6d96efc9Here is sample execution against JSONiq endpoint:
$ echo '[ [11,2,3], [4,5,6] ]' | coproc2 transpose.xq - http://dp6-l3:2226;echo
[
[
11,
4
],
[
2,
5
],
[
3,
6
]
]
$
Hermann.