2

我正在处理以下 xquery。对于每个工作室,我需要列出每个 dvd 标题及其评论描述。这是我到目前为止所拥有的。

<Studios>
{
for $studio in distinct-values(doc("dvdtitles.xml")//dvdstore/dvd/studioproduction)
return
<studio>
<studioproduction> {$studio} </studioproduction>
    <dvdtitles> 
    {
      for $dvd in doc("dvdtitles.xml")//dvd[studioproduction = $studio], $title in $dvd/title
      return $title 
    } </dvdtitles>
    <comments>
     {for $c in doc("dvdtitles.xml")//dvd[studioproduction = $studio],  $title in              $c/title,       $reviewTitle in doc("dvdtitles.xml")//review/title,  
     $y in //review/comments
      where $title = $reviewTitle
      return $y
   } </comments> 
    </studio> }
</Studios>

在 BaseX 中运行 xquery 后,我得到以下结果:

<Studios>
  <studio>
    <studioproduction>Universal Studios</studioproduction>
    <dvdtitles>
      <title>A.I. Artificial Intelligence</title>
    </dvdtitles>
    <comments>
      <comments>Amazing, visually stunning and thought-inspiring</comments>
      <comments>What a JOKE!</comments>
      <comments>A Disney film that deserves its Best Picture Nomination</comments>
      <comments>Disappointed</comments>
      <comments>This movie is insulting</comments>
      <comments>An excellent adaptation</comments>
      <comments>A tremendous disappointment</comments>
      <comments>YOU'LL FALL IN LOVE WITH "GHOST!"</comments>

我需要列出工作室和标题及其评论描述。我试图弄清楚如何在 xquery 的评论部分将它联系在一起。这就是我感到困惑的地方。

这是xml文档。

<?xml version="1.0"?>
<dvdstore>
    <dvd>
        <title>A.I. Artificial Intelligence</title>
        <releaseDate>2002-03-05</releaseDate>
        <studioproduction>Universal Studios</studioproduction>
        <rated>PG-13</rated>
        <regionCode>1</regionCode>
        <director>Steven Spielberg</director>
        <starring>Haley Joel Osment</starring>
        <starring>Jude Law</starring>
    </dvd>

        <dvd>
        <title>Beauty and the Beast</title>
        <releaseDate>2002-10-08</releaseDate>
        <studioproduction>Walt Disney Home Video</studioproduction>
        <rated>G</rated>
        <regionCode>4</regionCode>
        <director>Kril Wise</director>
        <director>Gary Trousdale</director>
        <starring>Paige O'Hara</starring>
        <starring>Robby Benson</starring>
    </dvd>

    <review>
        <title>A.I. Artificial Intelligence</title>
        <customer_rating>5</customer_rating>
        <reviewer>Andrew</reviewer>
        <reviewDate>2002-01-31</reviewDate>
        <location>San Diego, CA</location>
        <comments>Amazing, visually stunning and thought-inspiring</comments>
    </review>


    <review>
        <title>Beauty and the Beast</title>
        <customer_rating>5</customer_rating>
        <reviewer>George</reviewer>
        <reviewDate>2002-02-27</reviewDate>
        <location>Baltimore, MD</location>
        <comments>A Disney film that deserves its Best Picture Nomination</comments>
    </review>

</dvdstore>
4

1 回答 1

1

以下代码dvd按工作室名称对元素进行分组,并输出工作室名称和属于它的所有 dvd 标题。XPath/dvdstore/review[title = $dvd/title]/comments返回属于工作室任何标题的所有评论。

doc()请注意,我绑定了上下文项,而不是一直使用该函数。另一个可能存在文件实际上被多次打开的缺点(尽管优化器应该能够检测到并只打开一次)。

declare context item := doc("dvdtitles.xml");

<Studios>
{
for $dvd in /dvdstore/dvd
let $studio := $dvd/studioproduction
group by $studio
return
  <studio>
    <studioproduction>{$studio}</studioproduction>
    <dvdtitles>{$dvd/title}</dvdtitles>
    <comments>{
      /dvdstore/review[title = $dvd/title]/comments
    }</comments>
  </studio>
}
</Studios>
于 2014-05-08T14:40:38.987 回答