Where and how is $p defined or created from? Why is it the index or position of the node at all?
Input from w3schools, the bookstore example.
output:
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
query:
xquery version "3.1";
let $doc := db:open("bookstore")
let $books := $doc/bookstore/book
for $book at $p in $books
where $p eq 2
return $book
It's the "at" which assigns an index to $p? yet, my understanding is that xpath is declarative and not iterative. Yet, this at least resembles iteration. The "at" is not iteration, perhaps, but just the position of that node within that document?
see also:
C# What query language to specify in rule to pull out the two book nodes
for a related question.