Here is an improved (and shorter) version of the fold-left() solution:
fold-left($nodes, (), function($a, $n) { $a, $n except $a })
Michael Kay
Saxonica
On 28 Dec 2021, at 23:47, Michael Kay mike(_at_)saxonica(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
For a solution that delivers distinct nodes in order of first appearance, my
preference would be
$nodes => fold-left((), function($all, $this) {if ($all intersect $this) then
$all else ($all, $this)})
It's likely to be O(n^2) in most implementations, whereas Martin Honnen's
solution is probably O(n log n) -- but this one is XPath rather than XQuery,
and feels more elegant.
Michael Kay
Saxonica
On 28 Dec 2021, at 21:56, Michael Kay mike(_at_)saxonica(_dot_)com
<mailto:mike(_at_)saxonica(_dot_)com>
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:
You might consider
$nodes | ()
a bit more intuitive.
Michael Kay
Saxonica
On 28 Dec 2021, at 19:23, Eliot Kimber
eliot(_dot_)kimber(_at_)servicenow(_dot_)com
<mailto:eliot(_dot_)kimber(_at_)servicenow(_dot_)com>
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:
Hmph.
That is certainly much more efficient 😊 but is not necessarily obvious (at
least not to me).
Thanks!
E.
_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com <https://www.servicenow.com/>
LinkedIn <https://www.linkedin.com/company/servicenow> | Twitter
<https://twitter.com/servicenow> | YouTube
<https://www.youtube.com/user/servicenowinc> | Facebook
<https://www.facebook.com/servicenow>
From: Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de
<mailto:martin(_dot_)honnen(_at_)gmx(_dot_)de>
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>>
Date: Tuesday, December 28, 2021 at 1:15 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>>
Subject: Re: [xsl] XQuery/XPath 3.1: Node List to Node Set ("distinct
nodes")
[External Email]
On 28.12.2021 20:10, Eliot Kimber
eliot(_dot_)kimber(_at_)servicenow(_dot_)com
<mailto:eliot(_dot_)kimber(_at_)servicenow(_dot_)com> wrote:
I couldn’t find an answer in my google and markmail searching so I
thought I’d ask here:
Given an arbitrary list of nodes that may contain duplicates, what is
the most efficient way to reduce the node list to a set?
The solution I came up with is a recursive function:
(:
Get the unique nodes from the supplied sequence
@param nodes The sequence of nodes to evaluate
@return A sequence of nodes such that each node in $nodes exists exactly
once.
:)
declare function dutils:distinctNodes($nodes as node()*) as node()* {
dutils:_getDistinctNodes($nodes, ())
};
declare function dutils:_getDistinctNodes($nodes as node()*, $resultList
as node()*) as node()* {
if (exists($nodes))
then
let $node := head($nodes)
return dutils:_getDistinctNodes(tail($nodes), ($resultList | $node))
else $resultList
};
Which works but I feel like I’m missing some obvious way to do this more
directly, but I’m not seeing it.
Am I missing a better solution?
$nodes/.
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <applewebdata://3792D7E5-E104-4EE8-A576-A07AB4CDFB7B>)
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <applewebdata://3792D7E5-E104-4EE8-A576-A07AB4CDFB7B>)
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--