[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [XQuery] L09/S16
> Some questions about XQuery
>
> 1.
>
> I wonder if it is possible to solve the example discussed
> in Lecture 09 on Slide 16 by using the following query:
>
> 01 FOR
> 02 $book IN document("bib.xml")/bib/book,
> 03 $distinct_author IN DISTINCT-VALUES($book/author/text())
> 04
> 05 LET
> 06 $titles := $book[author/text()=$distinct_author]/title
> 07
> 08 RETURN
> 09 <answer>
> 10 <author> { $distinct_author } </author>
> 11 { $titles }
> 12 </answer>
yes, this will give the same result as the example on slide 16.
> 2.
>
> Is it possible to use the DISTINCT-VALUES function on
> elements or is explicitly its [text()] value required?
>
> Otherwise the above example would be simpler at
> some points:
>
> line 03: DISTINCT-VALUES($book/author)
> line 06: $book[author=$distinct_author]/title
> line 10: { $distinct_author }
>
> Of course, this way wouldn't work properly if we had
> special attributes for each author; different for some
> books like
>
> <author function="co-author"> efg </author>
>
> but as we consider the simple example on slide 4, it
> would be acceptable, I think.
distinct-values can be used on any type of nodes, not just on text nodes.
Thus the modified solution that you propose in 2. can work but the two
results will not be the same. With the first solution only text nodes for
authors will be considered, while with the solution in 2 any author node
will be considered. With respect to the bib.xml file on slide 4, in the
first case the author Rick Hull will not be included in the result while in
the second case he will be included.
> 3.
>
> To introduce conditions in iterations (slide 22), on every
> slide the same approach has been made, e.g.
>
> 01 FOR
> 02 $b IN document("bib.xml")/bib,
> 03 $a IN $b/book[author/text()]
> ...
>
> Can we use instead:
>
> 01 FOR
> 02 $b IN document("bib.xml")/bib/book,
> 03 $a IN $b[author/text()]
>
> Or does this result in a multiplication of rows, i.e.
>
> No. book x No. authors in each book ?
The difference between these two approaches is how the iterations in the two
cycles are defined. For example in the first case the outer cycle contains 1
iteration while in the second case the outer cycle has 2 iterations. Then if
you use for example LET expression as in 1. the colllections to which a
variable is binded can be different.