Eliminate duplicates in array (JSONiq)

前端 未结 3 1844
萌比男神i
萌比男神i 2020-12-20 01:25

I\'d like to delete duplicates in a JSONiq array.

let $x := [1, 2, 4 ,3, 3, 3, 1, 2, 5]

How can I eliminate the duplicates in $x?

3条回答
  •  一向
    一向 (楼主)
    2020-12-20 01:54

    regexp:replace is definitely not the way to go for this problem.

    While Matthias's solution does not work on Try Zorba, this script does:

    let $x := [1, 2, 4 ,3, 3, 3, 1, 2, 5]
    return [ distinct-values( jn:members($x) ) ]
    

    It returns (try out on above link):

    [ 1, 2, 3, 4, 5 ]
    

    Script needs to be a bit more verbose for DataPower Gateways JSONiq processor. There you get pretty-printed result for free:

    $ cat dv.xq 
    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    
    declare option output:method "json";
    declare option jsoniq-version "0.4.42";
    
    [ distinct-values( jn:members(.) ) ]
    $ 
    $ coproc2 dv.xq <(echo '[1, 2, 4 ,3, 3, 3, 1, 2, 5]') http://dp1-l3:2226; echo
    
    [
      1,
      2,
      4,
      3,
      5
    ]
    $ 
    

    Hermann.

提交回复
热议问题