{ let $ab := doc("address-book.xml") for $person in $ab//person return { for $contact-ref in $person//contact/@person-ref let $contact := $ab//person[@id = $contact-ref] return { $contact/firstname/text() } { $contact/surname/text() } } }