rldl
rldl

Reputation: 73

Understanding XQuery evaluation of a nested expression

I don't understand how to evaluate this expression. How will it evaluate to a number greater than 1, because the exists expression will always return 1 or 0?

for $a in distinct-values(doc("C:/BaseX/test/test1.xml")//author)

return
count(doc("C:/BaseX/test/test1.xml")//book
[exists(index-of(author, "Abiteboul"))]
)

text1.xml contains 5 books as the parent elements and authors as child elements.

Upvotes: 0

Views: 61

Answers (1)

Jens Erat
Jens Erat

Reputation: 38702

Please be aware that XQuery is not a procedural programming language, but a declarative one and you're still lacking knowledge of very basic language features like predicates, I'd recommend to work through some tutorial to get a grasp of the nature of the language.

Your query means "count all books where there exists an index-of author elements with the value Abiteboul", or resolving the semantics even further "count all books, which have an author of the name Abiteboul".

A semantically more reasonable code formatting would be to keep the brackets for the predicate in the lines above/below, and indent the predicate's contents.

for $author in distinct-values(doc("C:/BaseX/test/test1.xml")//author)
return
  count(doc("C:/BaseX/test/test1.xml")//book[
    exists(index-of(author, "Abiteboul"))
  ])

Anyway, the query seems a little bit complicated. Following query performs is equivalent, but much more concise:

for $author in distinct-values(doc("C:/BaseX/test/test1.xml")//author)
return
  count(doc("C:/BaseX/test/test1.xml")//book[author = "Abiteboul"])

Upvotes: 2

Related Questions