Xquery:根据多个值获取唯一块

Xquery: getting unique blocks with based on multiple values

我正在尝试从下面的XML

中获取一组独特的数据
<output>
  <category>DB</category>
  <title>Database systems</title>
  <name>Smith</name>
  <name>John</name>
  <name>Adam</name>
</output>
<output>
  <category>DB</category>
  <title>Database systems</title>
  <name>John</name>
  <name>Smith</name>
  <name>Adam</name>
</output>
<output>
  <category>DB</category>
  <title>Database systems</title>
  <name>Adam</name>
  <name>Smith</name>
  <name>John</name>
</output>
<output>
  <category>Others</category>
  <title>Pattern Recognition</title>
  <name>Adam</name>
  <name>Jeff</name>
</output>
<output>
  <category>Others</category>
  <title>Pattern Recognition</title>
  <name>Jeff</name>
  <name>Adam</name>
</output>

由于3个输出块包含相同的信息,我只需要选择一个。 但是,当我使用 distinct-values() 函数时,我会按照各自的顺序获取所有三个值。

我已将上面的 table 指定为 $final,下面是我得到的

for $f in distinct-values($final)
return $f

产出

DBDatabase systemsSmithJohnAdam
DBDatabase systemsJohnSmithAdam
DBDatabase systemsAdamSmithJohn

预计

<output>
  <category>DB</category>
  <title>Database systems</title>
  <name>Smith</name>
  <name>John</name>
  <name>Adam</name>
</output>
<output>
  <category>Others</category>
  <title>Pattern Recognition</title>
  <name>Adam</name>
  <name>Jeff</name>
</output>

无需订购 我试图对名称标签进行排序,但它没有成功,因为它向代码添加了太多内容。 Xquery 中是否有任何逻辑可以从上述 XML 中获取一份副本?

一个选项可以是:

doc("data.xml")//output/*[not(preceding::*=.)]

输出:

<category>DB</category>
<title>Database systems</title>
<name>Smith</name>
<name>John</name>
<name>Adam</name>

在您的实际 xml:

上按照这些思路尝试一些事情
let $inv :=
<doc>
 [your xml above]
</doc>
let $titles := $inv//output/title
for $title in distinct-values($titles)
return $inv//output[title[$title]][1]

输出:

<output>
  <category>DB</category>
  <title>Database systems</title>
  <name>Smith</name>
  <name>John</name>
  <name>Adam</name>
</output>
<output>
  <category>Others</category>
  <title>Pattern Recognition</title>
  <name>Adam</name>
  <name>Jeff</name>
</output>

在 XQuery 3 中,我认为最短且最有效的是使用 group by:

for $output in //output
group by $title := $output/title
return head($output)

https://xqueryfiddle.liberty-development.net/jyH9Xv5