0

xml ファイルに対して xquery を実行しようとしています。これは私の入力xmlファイルです:

<?xml version="1.0" ?>
<authors>
   <author><name> Steven</name>
     <title>advanced</title>
     <title>TCPIP</title>
   </author>
   <author><name> George</name>
     <title>DataMining</title>
     <title>Economic</title>
   </author>
   <author><name> Den</name>
    <title>TCPIP</title>
    <title>Economic</title>
   </author>
</authors>

各タイトルをグループ化し、各タイトルの著者数を数えようとしています。書籍は、著者の数が多い順に並べてください。各本について、そのタイトル、著者の数、すべての著者の名前をアルファベット順に出力します。最終的に、出力は次のようになります。

<bib>
 <book authors="1">
  <title>Advanced</title>
  <author>Steven</authhor>
 </book>
 <book authors="1">
  <title>DataMining</title>
  <author>George</author>
 </book>
 <book authors="2">
  <title>TCPIP</title>
  <author>Den</author>
  <author>Steven</author>
 </book>
 <book authors="2">
  <title>Economic</title>
  <author>Den</author>
  <author>George</author>
 </book>
</bib>

これは機能しない私のコードです:(。何か助けはありますか。可能であれば、新しいコードを書く代わりにコードを修正してください。答えを私の答えのようにしたいからです。

<bib>
{for $a in doc('test')//authors
let $T:={for $c in doc ('test')//authors/author[title=$a/author/title]
order by count($T)
return <book authors="{count($T)}">
{<title> {distinct-values($T/title)}
for $x in $T
order by $x/name()
return <author>{$x/name()}</author>}
</book>}
</bib>

コマンドを使おうとする部分に問題があると思いますlet。私が知っているように、 let コマンドでデータをグループ化できます。XML ファイルのタイトルごとにすべての著者を取得し、その後、著者の数を数えて名前で並べ替えようとしました。http://basex.org/products/live-demo/のリンクを使用して、回答をテストします。

4

1 に答える 1

1

Daniel Haley が提案したように、逆の方法で行う必要があります。つまり、各タイトルを反復して、この特定のタイトルを書いた著者を探します。distinct-valuesまたはを使用できますgroup by。以下が機能するはずです。

<bib>
{
  let $titles := distinct-values(doc('test')//title)
  for $t in $titles
  let $authors := doc('test')//author[title = $t]
  order by count($authors)
  return
    <book authors="{count($authors)}">
      <title>{$t}</title>
      {
        for $a in $authors
        order by $a/name
        return <author>{$a/name/string()}</author>
      }
    </book>
}
</bib>
于 2013-12-04T11:17:55.070 に答える