2
votes

I am working on the following xquery. For each studio, I need to list each dvd title with its review description. Here is what I have so far.

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

After running the xquery in BaseX, I get the following results:

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

I need to list studio and title with its review description. I am trying to figure out how to tie it together in the comments section of the xquery. That is where I am getting confused.

Here is the xml document.

<?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>
1
I don't quite get what you want as a result (what do you want to tie together and how?). Do you really simply want each review comment associated with a studio outputted in the <comments/> element? Shouldn't it be somehow associated to a title? Could you please post some expected output? As I don't know the exact question it is not answerable to me at the moment, but it sounds an awful lot like you actually want to use group by: docs.basex.org/wiki/XQuery_3.0#Group_Bydirkk
In my output, I want each studio with its corresponding movie title and the review description. For example, <studioproduction> Universal Studios </studioproduction> <title> A.I. Artificial Intelligence </title> <comments> Amazing, visually stunning and thought inspiring </comments>user3614649
sigh This does help much, none at all. You did not supply any additional information... I just answered something, but it is more guessing what it probably should do, not really knowing it.dirkk

1 Answers

1
votes

The following code groups your dvd elements by the studio name and outputs the studio name and all the dvd titles belonging to it. The XPath /dvdstore/review[title = $dvd/title]/comments returns all all reviews belonging to any of the titles of the studio.

Please note, that instead of using all the time the doc() function I bound the context item. The other one might have the drawback that the file is actually opened multiples times (although the optimizer should be able to detect that and just open it once).

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>