XQuery 3 merging two node sequences and de-duplicating





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















In XQuery 3.1 (under eXistDB 4.4) I have two functions which return two sets of nodes that describe family relations of a person. The results of the two functions can overlap.



A first function person:person-relationship-as-object("#Guilhem_Faure_SML-AU") returns:



<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Martin_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>

<person relation="Raimund_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>


A second function person:person-relationship-as-subject("#Guilhem_Faure_SML-AU") returns



<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Bernarda_Guilhem_Faure_SML-AU">
<span class="en">Spouse(s)</span>
<span class="fr">Époux/épouse(s)</span>
</person>


Now I want to merge the two sets of nodes into one, and return each distinct node only once.



I attempted a rough SQL-like structure, but failed:



let $x := person:person-relationship-as-object("#Guilhem_Faure_SML-AU") 
| person:person-relationship-as-subject("#Guilhem_Faure_SML-AU")

for $y in $x

order by $y/@relation ascending

group by $y/@relation, $y/span[@class="en"], $y/span[@class="fr"]

return $y


I don't think I really understand why the group by doesn't work here. Moreover, if group by doesn't do the job, how else can I approach what (I think?) should be a simple exercise in grouping/distinct results?










share|improve this question

























  • return functx:distinct-deep($x) does the job, without grouping...

    – jbrehr
    Nov 23 '18 at 16:40


















1















In XQuery 3.1 (under eXistDB 4.4) I have two functions which return two sets of nodes that describe family relations of a person. The results of the two functions can overlap.



A first function person:person-relationship-as-object("#Guilhem_Faure_SML-AU") returns:



<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Martin_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>

<person relation="Raimund_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>


A second function person:person-relationship-as-subject("#Guilhem_Faure_SML-AU") returns



<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Bernarda_Guilhem_Faure_SML-AU">
<span class="en">Spouse(s)</span>
<span class="fr">Époux/épouse(s)</span>
</person>


Now I want to merge the two sets of nodes into one, and return each distinct node only once.



I attempted a rough SQL-like structure, but failed:



let $x := person:person-relationship-as-object("#Guilhem_Faure_SML-AU") 
| person:person-relationship-as-subject("#Guilhem_Faure_SML-AU")

for $y in $x

order by $y/@relation ascending

group by $y/@relation, $y/span[@class="en"], $y/span[@class="fr"]

return $y


I don't think I really understand why the group by doesn't work here. Moreover, if group by doesn't do the job, how else can I approach what (I think?) should be a simple exercise in grouping/distinct results?










share|improve this question

























  • return functx:distinct-deep($x) does the job, without grouping...

    – jbrehr
    Nov 23 '18 at 16:40














1












1








1








In XQuery 3.1 (under eXistDB 4.4) I have two functions which return two sets of nodes that describe family relations of a person. The results of the two functions can overlap.



A first function person:person-relationship-as-object("#Guilhem_Faure_SML-AU") returns:



<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Martin_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>

<person relation="Raimund_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>


A second function person:person-relationship-as-subject("#Guilhem_Faure_SML-AU") returns



<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Bernarda_Guilhem_Faure_SML-AU">
<span class="en">Spouse(s)</span>
<span class="fr">Époux/épouse(s)</span>
</person>


Now I want to merge the two sets of nodes into one, and return each distinct node only once.



I attempted a rough SQL-like structure, but failed:



let $x := person:person-relationship-as-object("#Guilhem_Faure_SML-AU") 
| person:person-relationship-as-subject("#Guilhem_Faure_SML-AU")

for $y in $x

order by $y/@relation ascending

group by $y/@relation, $y/span[@class="en"], $y/span[@class="fr"]

return $y


I don't think I really understand why the group by doesn't work here. Moreover, if group by doesn't do the job, how else can I approach what (I think?) should be a simple exercise in grouping/distinct results?










share|improve this question
















In XQuery 3.1 (under eXistDB 4.4) I have two functions which return two sets of nodes that describe family relations of a person. The results of the two functions can overlap.



A first function person:person-relationship-as-object("#Guilhem_Faure_SML-AU") returns:



<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Martin_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>

<person relation="Raimund_de_Verazilh_SML-AU">
<span class="en">Aunt(s)/Uncle(s)</span>
<span class="fr">Tante(s)/Oncle(s)</span>
</person>


A second function person:person-relationship-as-subject("#Guilhem_Faure_SML-AU") returns



<person relation="Arnald_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Peire_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Esteve_Faure_SML-AU">
<span class="en">Sibling(s)</span>
<span class="fr">Frère(s)/sœur(s)</span>
</person>

<person relation="Bernarda_Guilhem_Faure_SML-AU">
<span class="en">Spouse(s)</span>
<span class="fr">Époux/épouse(s)</span>
</person>


Now I want to merge the two sets of nodes into one, and return each distinct node only once.



I attempted a rough SQL-like structure, but failed:



let $x := person:person-relationship-as-object("#Guilhem_Faure_SML-AU") 
| person:person-relationship-as-subject("#Guilhem_Faure_SML-AU")

for $y in $x

order by $y/@relation ascending

group by $y/@relation, $y/span[@class="en"], $y/span[@class="fr"]

return $y


I don't think I really understand why the group by doesn't work here. Moreover, if group by doesn't do the job, how else can I approach what (I think?) should be a simple exercise in grouping/distinct results?







xquery exist-db






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 16:00







jbrehr

















asked Nov 23 '18 at 15:02









jbrehrjbrehr

134212




134212













  • return functx:distinct-deep($x) does the job, without grouping...

    – jbrehr
    Nov 23 '18 at 16:40



















  • return functx:distinct-deep($x) does the job, without grouping...

    – jbrehr
    Nov 23 '18 at 16:40

















return functx:distinct-deep($x) does the job, without grouping...

– jbrehr
Nov 23 '18 at 16:40





return functx:distinct-deep($x) does the job, without grouping...

– jbrehr
Nov 23 '18 at 16:40












1 Answer
1






active

oldest

votes


















2














With XQuery and group by, the variable $y in your return clause is bound to the whole group you have created so to eliminate duplicates you need to use return $y[1]. See https://www.w3.org/TR/xquery-31/#id-group-by which says




In the post-grouping tuple generated for a given group, each
non-grouping variable is bound to a sequence containing the
concatenated values of that variable in all the pre-grouping tuples
that were assigned to that group.




and then




This behavior may be surprising to SQL programmers, since SQL reduces
the equivalent of a non-grouping variable to one representative value.







share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448966%2fxquery-3-merging-two-node-sequences-and-de-duplicating%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    With XQuery and group by, the variable $y in your return clause is bound to the whole group you have created so to eliminate duplicates you need to use return $y[1]. See https://www.w3.org/TR/xquery-31/#id-group-by which says




    In the post-grouping tuple generated for a given group, each
    non-grouping variable is bound to a sequence containing the
    concatenated values of that variable in all the pre-grouping tuples
    that were assigned to that group.




    and then




    This behavior may be surprising to SQL programmers, since SQL reduces
    the equivalent of a non-grouping variable to one representative value.







    share|improve this answer






























      2














      With XQuery and group by, the variable $y in your return clause is bound to the whole group you have created so to eliminate duplicates you need to use return $y[1]. See https://www.w3.org/TR/xquery-31/#id-group-by which says




      In the post-grouping tuple generated for a given group, each
      non-grouping variable is bound to a sequence containing the
      concatenated values of that variable in all the pre-grouping tuples
      that were assigned to that group.




      and then




      This behavior may be surprising to SQL programmers, since SQL reduces
      the equivalent of a non-grouping variable to one representative value.







      share|improve this answer




























        2












        2








        2







        With XQuery and group by, the variable $y in your return clause is bound to the whole group you have created so to eliminate duplicates you need to use return $y[1]. See https://www.w3.org/TR/xquery-31/#id-group-by which says




        In the post-grouping tuple generated for a given group, each
        non-grouping variable is bound to a sequence containing the
        concatenated values of that variable in all the pre-grouping tuples
        that were assigned to that group.




        and then




        This behavior may be surprising to SQL programmers, since SQL reduces
        the equivalent of a non-grouping variable to one representative value.







        share|improve this answer















        With XQuery and group by, the variable $y in your return clause is bound to the whole group you have created so to eliminate duplicates you need to use return $y[1]. See https://www.w3.org/TR/xquery-31/#id-group-by which says




        In the post-grouping tuple generated for a given group, each
        non-grouping variable is bound to a sequence containing the
        concatenated values of that variable in all the pre-grouping tuples
        that were assigned to that group.




        and then




        This behavior may be surprising to SQL programmers, since SQL reduces
        the equivalent of a non-grouping variable to one representative value.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 20:41

























        answered Nov 23 '18 at 19:55









        Martin HonnenMartin Honnen

        113k66279




        113k66279
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448966%2fxquery-3-merging-two-node-sequences-and-de-duplicating%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini