在 XQuery 中计算不同的多个值TCP/IP 图解</ti...</desc> <question vote="0"> <p>给出下面的 XML 文档。这个 XML 文档包含很多书籍。书籍可以由许多作者撰写: *</p> <pre><code>`<bib> <book year="1994"> <title>TCP/IP Illustrated</title> <author> <last>Stevens</last> <first>W.</first> </author> <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="1992"> <title>Advanced Programming in the UNIX Environment</title> <author> <last>Stevens</last> <first>W.</first> </author> <publisher>Addison-Wesley</publisher> <price>39.95</price> </book> <book year="2000"> <title>Data on the Web</title> <author> <last>Abiteboul</last> <first>Serge</first> </author> <author> <last>Buneman</last> <first>Peter</first> </author> <author> <last>Suciu</last> <first>Dan</first> </author> <publisher>Morgan Kaufmann Publishers</publisher> <price>39.95</price> </book> <book year="1999"> <title>The Economics of Technology and Content for Digital TV</title> <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> </book> </bib>` </code></pre> <ul> <li></li> </ul> <p>我想从这个文档中输出没有重复的作者数量。</p> <p>我尝试过以下方法:</p> <pre><code>let $x:= distinct-values(doc('biblio.xml')//author/(first, last)) return count ($x) </code></pre> <p>但是不幸的是,这个查询没有给我预期的输出。</p> </question> <answer tick="false" vote="0"> <p>您可以按 <pre><code>author</code></pre> 和 <pre><code>first</code></pre> 元素对 <pre><code>last</code></pre> 元素进行分组,然后对组进行计数:</p> <pre><code>(for $author in //author group by $first := $author/first, $last := $author/last return $author[1]) => count() </code></pre> </answer> <answer tick="false" vote="0"> <p>尝试将 xpath 表达式更改为</p> <pre><code>distinct-values(//author/concat(last, first)) </code></pre> <p>应该根据您的样本输出<pre><code>4</code></pre></p> </answer> </body></html>

问题描述 投票:0回答:0
xml xquery xquery-3.0
© www.soinside.com 2019 - 2024. All rights reserved.