简体   繁体   English

XQuery 获得不同的值

[英]XQuery get distinct values

Im new to XQuery and are trying to learn how to use it with BaseX.我是 XQuery 的新手,正在尝试学习如何将它与 BaseX 一起使用。

I have this xml containing multiple elements of program :我有这个 xml 包含程序的多个元素:

<program id="4565">
        <name>Musiken i P3</name>
        <url>http://sverigesradio.se/sida/default.aspx?programid=4565</url>
        <editor>Anna-Karin Larsson</editor>
        <channel>164</channel>
        <category>5</category>
</program>

So far i have understod that i can get the distinct values of for example editor like such:到目前为止,我已经理解我可以获得例如编辑器的不同值,如下所示:

let $vals := $sr/sr/programs/program/editor
let $uniqe-items := distinct-values($vals)

for $vals in $uniqe-items
    order by $vals
    return <li>{string($vals)}</li>

However when i do this I only get the editors name and I can't understand how i also check something like this:然而,当我这样做时,我只得到编辑的名字,我不明白我是如何检查这样的:

 let $vals := $sr/sr/programs/program/editor
 let $uniqe-items := distinct-values($vals)

 for $vals in $uniqe-items
     where $vals/channel = "132"
     order by $vals
     return <li>{string($vals)}</li>

I was thinking that i could do something like $vals/../channel to get back to the channel attribute but that doesn't seem to be the case.我在想我可以做一些类似$vals/../channel的事情来回到通道属性,但似乎并非如此。

Please try the following solution.请尝试以下解决方案。 I am using BaseX 9.5我正在使用 BaseX 9.5

The distinct-values() gives a sequence of strings: Anna-Karin Larsson, Anna Karenina, not the XML elements. distinct-values()给出一个字符串序列:Anna-Karin Larsson、Anna Karenina,而不是 XML 元素。

It is possible to eliminate where clause completely by using XPath predicate in the for clause:for子句中使用 XPath 谓词可以完全消除where子句:

for $vals in distinct-values(./programs/program[channel = "132"]/editor)

XQuery XQuery

xquery version "3.1";

declare context item := document {
<programs>
    <program id="4565">
        <name>Musiken i P3</name>
        <url>http://sverigesradio.se/sida/default.aspx?programid=4565</url>
        <editor>Anna-Karin Larsson</editor>
        <channel>164</channel>
        <category>5</category>
    </program>
    <program id="4770">
        <name>Musiken i P3</name>
        <url>http://sverigesradio.se/sida/default.aspx?programid=4565</url>
        <editor>Anna Karenina</editor>
        <channel>132</channel>
        <category>55</category>
    </program>
</programs>  
};

<ul>
{
  for $vals in distinct-values(./programs/program[channel = "132"]/editor)
  (:where ./programs/program[editor = $vals]/channel = "132":)
  order by $vals
  return <li>{data($vals)}</li>
}
</ul>

Output Output

<ul>
  <li>Anna Karenina</li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM