xsl-list
[Top] [All Lists]

Re: [xsl] XQuery/XPath 3.1: Node List to Node Set ("distinct nodes")

2021-12-28 17:36:26
To see that the original sequence order is preserved in the result, here is
a complete example:

let $xml := parse-xml("<t><a/><b/><c/></t>"),
    $nodes := ($xml/*/a, $xml/*/c, $xml/*/b, $xml/*/a, $xml/*/b ),
    $ids := distinct-values($nodes ! generate-id(.))
 return  $ids ! (function($id) {$nodes[generate-id(.) eq $id][1]})(.)

produces:

<a/>
<c/>
<b/>



On Tue, Dec 28, 2021 at 3:32 PM Dimitre Novatchev 
dnovatchev(_at_)gmail(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:



On Tue, Dec 28, 2021 at 3:29 PM Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com>
wrote:

A pure XPath solution:

let $ids := distinct-values($nodes ! generate-id(.))
   return  $ids ! (function($id) {$nodes[generate-id(.) eq $id][1]})(.)

Happy New Year to all !



Hit Send too early:

Do notice: this seems the only solution of all presented so far, that
preserves the original sequence order (not document order) of the nodes.




On Tue, Dec 28, 2021 at 2:39 PM Wendell Piez 
wapiez(_at_)wendellpiez(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Eliot and XSL-List,

Along similar lines to $nodes | (), consider

$nodes except ()
$nodes intersect $nodes

I'm finding it difficult to argue what's most intuitive but I kind of
like 'intersect'. Can't vouch for performance though. :-)

Best wishes for 2022 --
Wendell




On Tue, Dec 28, 2021 at 4:56 PM Michael Kay mike(_at_)saxonica(_dot_)com <
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 <
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 <
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 
<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 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)


XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/174322> (by
email)



--
...Wendell Piez... ...wendell -at- nist -dot- gov...
...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
email)



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/782854> (by
email <>)



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
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
--~--
<Prev in Thread] Current Thread [Next in Thread>