Subject:XQuery beginner problem Author:John Towers Date:09 Mar 2007 09:02 AM Originally Posted: 09 Mar 2007 08:39 AM
Hi guys, I'm a beginner at XQuery and I'm coming at it from a relational SQL perspective, and I think it's tripping me up. I'm trying to use the following query, to retrieve all the titles of books that have a publication date of 1959 or 1939, and the ratings of all movies where the directors go by Michel Gondry or Edgar Wright:
for $book in //book
for $film in //film
where ($book/yearofPublication = '1959'
or $book/yearofPublication = '1932')
and ($film/director = 'Michel Gondry'
or $film/director = 'Edgar Wright')
return ($book/ title, $film/ rating)
In my application the above query is returning the following results
<result>Naked Lunch</result>
<result>PG</result>
<result>Naked Lunch</result>
<result>15</result>
<result>Brave New World</result>
<result>PG</result>
<result>Brave New World</result>
<result>15</result>
<result>Brave New World</result>
<result>PG</result>
<result>Brave New World</result>
<result>15</result>
The problem is the repetition, I'm getting results returned for each iteration through the XML? Can anyone show me how I can re-write this query to get the results I'm after, while also getting rid of that repetition? I understand that the results I am getting are caused by returning values based on the two 'for' iterations through the tree, but can't figure out how to fix it so that the relevant values are only returned once.