【问题标题】:Xquery - trouble binding the third requirementXquery - 绑定第三个要求的麻烦
【发布时间】:2014-05-08 03:23:48
【问题描述】:

我正在处理以下 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 的 cmets 部分中将其绑定在一起。这就是我感到困惑的地方。

这是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>

【问题讨论】:

  • 结果我不太明白你想要什么(你想把什么联系起来,如何联系起来?)。您真的只是希望在&lt;comments/&gt; 元素中输出与工作室相关联的每条评论吗?它不应该以某种方式与标题相关联吗?您能否发布一些预期的输出?由于我不知道确切的问题,目前我无法回答,但这听起来很像你真的想使用group by:docs.basex.org/wiki/XQuery_3.0#Group_By
  • 在我的输出中,我希望每个工作室都有其对应的电影标题和评论描述。例如, 环球影城 A.I.人工智能 令人惊叹、视觉震撼和启发灵感
  • sigh 这确实有很大帮助,一点也没有。你没有提供任何额外的信息......我只是回答了一些问题,但更多的是猜测它可能应该做什么,而不是真正知道它。

标签: xml xquery basex


【解决方案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>

【讨论】:

  • 您的修改解决了这个问题。查询结果显示属于特定工作室的每个标题的 cmets。
猜你喜欢
  • 1970-01-01
  • 2011-07-15
  • 2013-05-24
  • 2015-07-15
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多