2014-05-08 17 views
2

我正在处理下面的xquery。对于每个工作室,我需要列出每个DVD标题及其评论描述。这是我到目前为止。Xquery - 绑定第三个需求的问题

<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> 
+0

我不完全明白你想要什么结果(你想要什么结合起来以及如何?)。你真的只是想要在''元素中输出的与演播室相关的每个评论评论?它不应该以某种方式与标题相关联吗?你能否发布一些预期的产出?由于我不知道确切的问题,目前我无法对此负责,但听起来很像您真正想使用'group by':https://docs.basex.org/wiki/XQuery_3。 0#Group_By – dirkk

+0

在我的输出中,我希望每个工作室都有相应的电影标题和评论说明。例如,环球影城 A.I.人工智能令人惊叹的,视觉上令人惊叹的和令人鼓舞的 user3614649

+0

_sigh_这确实有很大帮助,根本没有帮助。你没有提供任何额外的信息......我只是回答了一些事情,但它更像是应该做什么,而不是真正了解它。 – dirkk

回答

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> 
+0

您的修订版解决了这个问题。查询结果为属于特定工作室的每个标题提出评论。 – user3614649