XQuery 获取不同的值
XQuery get distinct values
我是 XQuery 的新手,正在尝试学习如何将它与 BaseX 结合使用。
我有这个 xml 包含 program 的多个元素:
<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>
到目前为止,我已经了解我可以获得例如这样的编辑器的不同值:
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>
然而,当我这样做时,我只得到编辑的名字,我不明白我怎么也检查这样的东西:
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>
我在想我可以做类似 $vals/../channel
的事情来返回频道属性,但事实似乎并非如此。
请尝试以下解决方案。我正在使用 BaseX 9.5
distinct-values()
给出了一个字符串序列:Anna-Karin Larsson、Anna Karenina,而不是 XML 个元素。
可以通过在 for
子句中使用 XPath 谓词来完全消除 where
子句:
for $vals in distinct-values(./programs/program[channel = "132"]/editor)
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>
输出
<ul>
<li>Anna Karenina</li>
</ul>
我是 XQuery 的新手,正在尝试学习如何将它与 BaseX 结合使用。
我有这个 xml 包含 program 的多个元素:
<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>
到目前为止,我已经了解我可以获得例如这样的编辑器的不同值:
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>
然而,当我这样做时,我只得到编辑的名字,我不明白我怎么也检查这样的东西:
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>
我在想我可以做类似 $vals/../channel
的事情来返回频道属性,但事实似乎并非如此。
请尝试以下解决方案。我正在使用 BaseX 9.5
distinct-values()
给出了一个字符串序列:Anna-Karin Larsson、Anna Karenina,而不是 XML 个元素。
可以通过在 for
子句中使用 XPath 谓词来完全消除 where
子句:
for $vals in distinct-values(./programs/program[channel = "132"]/editor)
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>
输出
<ul>
<li>Anna Karenina</li>
</ul>