There are multiple smaller problems with your codes.
- Names in authors and books XML files do not match. I guess it's only some typos.
- Predicates belong to an axis step, not in their own (remove the slash after
book
in line one). - XML and XQuery are capitalization sensitive!
<Book/>
uses a capital B, so do the same in your XQuery, again in line 1. - In Line two, you're looping over all
<authorRoot/>
elements. UseauthorRoot/author
instead. - In line three you're hiding the book
$p
with it's name for the rest of this FLWOR expression, but you want to use the book again in line five. Better use another variable name. - Better do not use the
descendant-or-self
-step//
if you don't need it (lines one and two). This decreases performance.
I don't get what your idea for filtering was in lines three to five. Compare yourself with this working solution. Additionally I used speaking variable names, don't confuse yourself with unnecessary short ones.
Replace $book
and $authors
by the respecting doc(...)
functions.
for $book in $books//library/Book[title/contains(., 'Title1')]
for $author in $authors//authorRoot/author
where $book/author = $author/Name
return $author/Location/text()
If you want to have a list of distinct places, wrap distinct-values(...)
around all four lines.
An alternative without explicit loops:
$authors/authorRoot/author[
Name = $books/library/Book[contains(title, 'Title1')]/author)
]/Location
The second solution is also valid XPath 1.0, the first requires an XPath 3.0 or XQuery processor.