XQuery 3 merging two node sequences and de-duplicating
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
add a comment |
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
return functx:distinct-deep($x) does the job, without grouping...
– jbrehr
Nov 23 '18 at 16:40
add a comment |
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
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
xquery exist-db
edited Nov 23 '18 at 16:00
asked Nov 23 '18 at 15:02
jbrehr
115212
115212
return functx:distinct-deep($x) does the job, without grouping...
– jbrehr
Nov 23 '18 at 16:40
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 23 '18 at 20:41
answered Nov 23 '18 at 19:55
Martin Honnen
111k65876
111k65876
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
return functx:distinct-deep($x) does the job, without grouping...
– jbrehr
Nov 23 '18 at 16:40